Monday, January 28, 2008

AJAX tutorial 1

I finally started looking at AJAX and it seems pretty easy (well if you know html, javascript, already) it makes your learning curve even less steep if you knew actionscript 2.0 because there are many similar elements.

anyways, keys to understanding is XMLHttpRequest and DOM

DOM is basically way of knowing each elements location/names by browser.
1. window is window
2. document is what's inside the window
3. your a object will be like document.object

so document.getElementById('idOfTheObject') will give you access to that object. if you know css yes it's that same id you use. instead of # before the id name you use document.getElementById to access the object.

XMLHttpRequest is something that browser does with server. browsers are communicating with server and this is just and object that tracks that communication.

and you have methods to access that communication like onreadystatechange
this is used like xhr.onreadystatechange = function () {} or xhr.onreadystatechange = functionName; when xhr is the XMLHttpRequest you've created.

so how do you create XMLHttpRequest object? so that i can get info on what the page is doing with the server..
simple..

xhr = new XMLHttpRequest();

like you would any other object...

Only thing you need to keep track of is if the browser know what XMLHttpRequest object is
since this is relatively new technology you wouldn't be able to find one if somebody is using a computer from 10 years ago without updating.

so you just check
if (window.XMLHttpRequest)

very simple. and then in that if {} you are going to create XHR object.

var xhr = false;

if (window.XMLHttpRequest)
{
xhr = new XMLHttpRequest();
}


that will create the object and you can get ready for the next step.

No comments: