AJAX: The Simplest & Most Effective Method

Discussion in 'JavaScript' started by -NB-, Feb 10, 2007.

  1. #1
    This is a quick little snippet of AJAX for you eager users out there. It is cross-browser compatible, and is definitely some of the cleanest code available.

    var xml = createXML();
    var el = new Array();
    
    function createXML () {
    	if (typeof XMLHttpRequest == 'undefined') {
    		xmlObjects = Array(
    			'Microsoft.XmlHttp',
    			'MSXML2.XmlHttp',
    			'MSXML2.XmlHttp.3.0',
    			'MSXML2.XmlHttp.4.0',
    			'MSXML2.XmlHttp.5.0'
    		);
    		for (i in xmlObjects) {
    			try {
    				return new ActiveXObject(objects[i]);
    			} catch (e) {}
    		}
    	} else {
    		return new XMLHttpRequest();
    	}
    }
    function get (id) {
    	return document.getElementById(id);
    }
    function getPage (url, element) {
    	el['results'] = get(element);
    	xml.open('get', url);	
    	xml.onreadystatechange = function () {
    		if (xml.readyState == 4) {
    			el['results'].innerHTML = xml.responseText;
    		} else {
    			el['results'].innerHTML = 'Loading…';
    		}		
    	}
    	xml.send(null);
    }
    function postPage (url, parameters, element) {
    	el['results'] = get(element);
    	xml.open('post', url);
    	xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
    	xml.onreadystatechange = function () {
    		if (xml.readyState == 4) {
    			el['results'].innerHTML = xml.responseText;
    		} else {
    			el['results'].innerHTML = 'Loading…';
    		}		
    	}
    	xml.send(window.encodeURI(parameters));
    }
    Code (markup):
    If you enjoyed this code please feel free to leave rep or comments (maybe both :eek:) :)
     
    -NB-, Feb 10, 2007 IP
  2. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #2
    Yes thats a clean and easy to use code, but you should also post some code usage example for newbies here.
     
    designcode, Feb 11, 2007 IP
  3. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #3
    what does it do? what is it for? how to implement... details please
     
    bobby9101, Feb 11, 2007 IP
  4. rays

    rays Active Member

    Messages:
    563
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #4
    this is nice code... but i guess it will confuse newbies more ... try and put lot of comments and sample usage ... i hope after this you will get some good green reps
     
    rays, Feb 12, 2007 IP
  5. ZeRo_CoOl

    ZeRo_CoOl Peon

    Messages:
    635
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Very nice mate, weldone
     
    ZeRo_CoOl, Feb 12, 2007 IP
  6. MeetHere

    MeetHere Prominent Member

    Messages:
    15,399
    Likes Received:
    994
    Best Answers:
    0
    Trophy Points:
    330
    #6
    Same question, what does it do ?

    Can I see a working demo ? :)
     
    MeetHere, Feb 12, 2007 IP
  7. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #7
    Basically, with this code you can post variables to server and can catch response returned from server. I will try to post a practical implementation of this code tonight.
     
    designcode, Feb 12, 2007 IP
  8. TheException

    TheException Banned

    Messages:
    871
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks for sharing. I'm taking up learning Ajax myself actually. Hopefully I'll find a good book soon, it's nice to have a print book when learning any scripting or programming.
     
    TheException, Feb 12, 2007 IP
  9. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #9
    Hey TheException, but I think a short practical example or case study teaches you what you learn from 25 or 30 pages of book.
     
    designcode, Feb 12, 2007 IP
  10. 3l3ctr1c

    3l3ctr1c Peon

    Messages:
    380
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I'll get a tutorial on this and some other opensource scripts of mine by today night soon!
     
    3l3ctr1c, Feb 13, 2007 IP
  11. poseidon

    poseidon Banned

    Messages:
    4,356
    Likes Received:
    246
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Eagerly waiting ... :)
     
    poseidon, Feb 14, 2007 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    I guess:
    
    return new ActiveXObject(objects[i]);
    
    Code (javascript):
    Should be:
    
    return new ActiveXObject(xmlObjects[i]);
    
    Code (javascript):
    :)
     
    nico_swd, Feb 15, 2007 IP
  13. 3l3ctr1c

    3l3ctr1c Peon

    Messages:
    380
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #13
    There goes my tutorial on this

    http://www.hakc.net/2007/02/15/the-hello-world-ajax-script/
    Code (markup):
    Hope it's good and well explanatory!
     
    3l3ctr1c, Feb 15, 2007 IP
  14. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #14
    commandos, Feb 15, 2007 IP
  15. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #15
    Nice sharing commandos, thanks (y)
     
    designcode, Feb 15, 2007 IP
  16. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Gimme my thread back :p

    Thanks for all the comments and suggestions, guys! Sorry about the minor code error, check out nico_swd's post on the fix :)

    Thanks,
    Nick
     
    -NB-, Feb 15, 2007 IP