Archive for the ‘Javascript’ Category

Javascript quick tips to do common tasks

10
May
function setCookie (name, value, expires, path, domain, secure){

var today = new Date();

today.setTime(today.getTime());

if (expires){

expires = expires * 1000 * 60 * 60 * 24;

}

var expires_date = new Date( today.getTime() + (expires));

document.cookie = name+'='+escape( value ) +

((expires) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()

((path) ? ';path=' + path : '' ) +

((domain) ? ';domain=' + domain : '' ) +

((secure) ? ';secure' : '' );

}

function getCookie(name){

var start = document.cookie.indexOf( name + "=" );

var len = start + name.length + 1;

if ((!start) && (name != document.cookie.substring( 0, name.length))){

return null;

}

if (start == -1) return null;

var end = document.cookie.indexOf(';', len);

if (end == -1) end = document.cookie.length;

return unescape(document.cookie.substring(len, end));

}

function deleteCookie (name, path, domain){

if (getCookie(name)) document.cookie = name + '=' +

((path) ? ';path=' + path : '') +

((domain) ? ';domain=' + domain : '' ) +

';expires=Thu, 01-Jan-1970 00:00:01 GMT';

}

,

04
May

Initially, set onclick=”return false”, then use your script to change the value of onclick when the time comes.

You could also add a class to the anchor, then use CSS to style the link so that it does not look like a link. Then remove that class from the element at the same time that you change the value of onclick.

30
Apr
import java.lang.reflect.*;

Class c = ...;  // see above

Method[] methods = c.getMethods();

Method m = methods[i];

String s = m.getName();

Class r = m.getReturnType();  // returns void.class if void

Class[] p = m.getParameterTypes();

// to invoke a method, use:

Object invoke(Object o, Object[] args);

27
Apr
//// thread definition

function Thread( name ) {

for ( var i = 0; i < 5; i++ ) {

Print(name+': '+i);

yield;

}

}

//// thread management

var threads = [];

// thread creation

threads.push( new Thread('foo') );

threads.push( new Thread('bar') );

// scheduler

while (threads.length) {

var thread = threads.shift();

try {

thread.next();

threads.push(thread);

} catch(ex if ex instanceof StopIteration) {}

}

30
Mar

JavaScript has a built in function that allows you to work with dates. This feature is great for creating a calender, to display the current date, or to create a clock. You can also use the date function to set the date in your script.

Date Objects
Method Description
getDate() Returns the current date and time
getFullYear() Returns the four digit year
getYear() Returns the year in two digit format, ie 08
getUTCFullYear Returns the four digit year according to universal time.
getUTCYear Returns the year in two digit format according to universal time
getMonth Returns the month as a number, between 0 and 11
getUTCMonth Returns the month as a number according to universal time
getDate Returns the day of the month.
getUTCDate Returns the day of the month according to universal time
getDay() Returns the day of the week as a number between 0-6
getUTCDay() Returns the day of the week according to universal time

 

Time Objects
Method Description
getHour() Returns the hour in 24-hour format
getUTCHour() Returns the hour according to universal time in 24 hour format
getMinute() Returns the minute between 0 and 59
getUTCMinute() Returns the minute according to universal time
getSecond() Returns the second between 0 and 59
getUTCSecond() Returns the second according to universal time
getMillisecond() Returns the millisecond between 0 and 999
getUTCMillisecond() Returns the millisecond according to universal time
getTimezoneOffset() Returns the difference in minutes between GMT and local time

26
Mar

Returns the angle theta of x and y

atan2(y,x)

,

20
Mar

Returns the value of the highest number
max(x,y)

,

19
Mar

log(x)

Returns the natural logarithm of x

,

16
Mar

This code is used for moving  image in browser.

<html>
<h2>Move Image</h2>
<script type=”text/javascript”>
var X, Y;
window.document.onclick=moveImage;
function moveImage(){
X = (document.layers) ? e.pageX : event.clientX
Y = (document.layers) ? e.pageY : event.clientY
document.getElementById(‘image’).style.position=”absolute”;
document.getElementById(‘image’).style.left=X;
document.getElementById(‘image’).style.top=Y;
}
</script>
<img id=”image” src=”node.jpg”></img>
</html>

, ,

31
Jan

Due to the fact that the iPhone allow its users to view a page in both portrait and landscape modes, you may need to be able to detect in which mode the document is being read.
This handy javascript function will detect the current iPhone orientation and will apply a specific CSS class so you can style it your way.

window.onload = function initialLoad() {
    updateOrientation();
}

function updateOrientation(){
    var contentType = "show_";
    switch(window.orientation){
        case 0:
	contentType += "normal";
	break;

	case -90:
	contentType += "right";
	break;

	case 90:
	contentType += "left";
	break;

	case 180:
	contentType += "flipped";
	break;
    }
    document.getElementById("page_wrapper").setAttribute("class", contentType);
}