Description of the AJAX book
Buy our AJAX book at the best price!
Hacks for good prototyping of DOM elements with javascript in an Ajax manner
div.innerHTML = person.firstName+' '+person.lastName;
div.onclick = function() {
alert(this.innerHTML+' was clicked');
}
<div id="myGroup" style="border: 1px dashed #000; padding: 5px; width: 300px;"></div>
//A class Person
function Person( firstName, lastName ) {
this.firstName = firstName;
this.lastName = lastName;
}
//A function to be executed when onclick is fired
function clickFunction(person) {
alert(person.firstName+' '+person.lastName+' was clicked');
}
//We create 10 DIV representing 10 people
for (var i=0; i < 10; i++) {
//Create a DIV
var div = document.createElement('DIV');
//Create an object
var person = new Person('firstName'+i, 'lastName'+i);
//Fill in the DIV with the object infos
div.innerHTML = person.firstName+' '+person.lastName;
//Some styling
div.style.border = '1px solid #CCC';
div.style.margin = '5px';
div.style.cursor = 'pointer';
//Set the event on the DIV
div.onclick = function() {
//Use the object matched with the DIV in a function
clickFunction(person);
}
document.getElementById('myGroup').appendChild(div);
}
//A class Person
function Person( firstName, lastName ) {
this.firstName = firstName;
this.lastName = lastName;
}
//A function to be executed when onclick is fired
function clickFunction(person) {
alert(person.firstName+' '+person.lastName+' was clicked');
}
//A function that is used to create a DIV matched with an object
function createDiv(index) {
//Create a DIV
var div = document.createElement('DIV');
//Create an object
var person = new Person('firstName'+index, 'lastName'+index);
//Fill in the DIV with the object infos
div.innerHTML = person.firstName+' '+person.lastName;
//Some styling
div.style.border = '1px solid #CCC';
div.style.margin = '5px';
div.style.cursor = 'pointer';
//Set the event on the DIV
div.onclick = function() {
//Use the object matched with the DIV in a function
clickFunction(person);
}
document.getElementById('myGroup').appendChild(div);
}
//We create 10 DIV representing 10 people
for (var i=0; i < 10; i++) {
createDiv(i);
}
//Function to make DIV blink
this.blink = function() {
//Blink 10 times
this.times = (this.times-1)||10;
//If not 10 times yet
if ( this.times > 0 ) {
//Switch blink color
if ( this.times%2==0 )
this.style.backgroundColor = '#000';
else
this.style.backgroundColor = '#FFF';
this_ = this;
//Redo blink in 50 milliseconds
setTimeout('this_.blink()', 50);
else { //Times out, stop blinking, reset counter value
clearTimeout();
this.times = 10;
}
}
var dateInfos = (new Date()).toString().split(/ GMT|[ :]/);
var day = dateInfos[0];
var month = dateInfos[1];
var date = dateInfos[2];
var year = dateInfos[3];
var hour = dateInfos[4];
var minute = dateInfos[5];
var second = dateInfos[6];
var offsetUTC = dateInfos[7];
var dateInfos = (' '+(new Date()).toString()).split(/ GMT|[ :]/);
var day = dateInfos[0];
var month = dateInfos[1];
var date = dateInfos[2];
var year = dateInfos[3];
var hour = dateInfos[4];
var minute = dateInfos[5];
var second = dateInfos[6];
var offsetUTC = dateInfos[7];
//Repeat a string 'many' times
String.prototype.repeat = function(many) {
var s = '', t = this.toString();
while (--many >= 0) s += t;
return s;
}
//Left fill a number with zero so that the result has a length of 'many'
Number.prototype.addZero = function(many) {
return ('0'.repeat(many) + this.toString()).substring(('0'.repeat(many) + this.toString()).length - many);
}
var i = 5;
i.addZero(10); //You get the string '0000000005'
OR (new Number(5)).addZero(10);
BUT NOT 5.addZero(10);
Date.prototype.toFullString = function() {
return this.getDate().addZero(2) + '/' + (this.getMonth()+1).addZero(2) + '/' + this.getFullYear();
}
}
//A function that returns unique IDs
//(it's like a class with a class attribute)
getId = function() {
if (!this.id)
this.id = 0;
this.id++;
return this.id;
}
//A function that acts like a stack (registers objects by unique IDs)
registerObject = function(objType, id, o) {
if (!this.objById)
this.objById = new Array();
if (!this.objById[objType])
this.objById[objType] = new Array();
if (this.objById[objType][id])
alert('Object already registered with this ID!!');
else
this.objById[objType][id] = o;
}
//A function that unstacks the registered object with a given ID
getObjectById = function(objType, id) {
return this.objById[objType][id];
}
//We use objType for our stack to be reusable
//in other parts of our program
//This way, the stack can hold several object in different use contexts
//A test class to be instantiate
testClass = function() {
//Attribute
this.phrase = 'Cool! The context is saved!';
//Method with reference to the class object
this.doSomething = function() {
alert(this.phrase);
}
}
//The function that delay code execution
delay = function(code, contexte, time) {
//We provide a unique ID for our context object to be memorized
var id = getId();
//We register our context object in the stack
registerObject('delay', id, contexte);
//When time is out, the code will be evaluated within the stacked context
setTimeout('eval('+code+'.call(getObjectById(\'delay\','+id+')));', time);
}
//Then we run the test!!
//Instantiate the test class
var myTest = new testClass();
//Call a method right now
myTest.doSomething();
//Call the same method in 5 secondes
delay(function() { this.doSomething(); }, myTest, 5000);