SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    396
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Most efficient way to load another page using XMLHttpRequest?

    Hi guys,

    I've currently got this code that I use to load another page:

    Code JAVASCRIPT:
    function loadurl(dest) {
    	try {
    		// Moz supports XMLHttpRequest. IE uses ActiveX. browser detction is bad. object detection works for any browser
    		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    	} catch (e) {
    		// browser doesn't support ajax.
    	}
    	// the xmlhttp object triggers an event everytime the status changes - triggered() function handles the events
    	xmlhttp.onreadystatechange = triggered;
    	// open takes in the HTTP method and url.
    	xmlhttp.open("GET", dest);
    	// send the request. if this is a POST request we would have sent post variables: send("name=aleem&gender=male)
    	xmlhttp.send(null);
    }
    function triggered() {
    	// if the readyState code is 4 (Completed)  and http status is 200 (OK) we go ahead and get the responseText
    	// other readyState codes: 0=Uninitialised 1=Loading 2=Loaded 3=Interactive
    	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
    		// xmlhttp.responseText object contains the response.
    		document.getElementById("output").innerHTML = xmlhttp.responseText;
    	}
    }
    (taken from this tutorial)

    It works great, but requires an extra div to be placed on the page with the id of output. It's not a huge problem at all, but I was just wondering if there was another way of doing this since all the script needs to do is add something to the database, not output anything.

    Cheers

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    If there is no output then you can get rid of the whole triggered part.

    Code Javascript:
    function loadurl(dest) {
        try {
            // Moz supports XMLHttpRequest. IE uses ActiveX. browser detction is bad. object detection works for any browser
            xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            // browser doesn't support ajax.
        }
        // open takes in the HTTP method and url.
        xmlhttp.open("GET", dest);
        // send the request. if this is a POST request we would have sent post variables: send("name=aleem&gender=male)
        xmlhttp.send(null);
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    396
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That worked an absolute treat, thank you so much once again pmw57 - really appreciate it


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •