Discover Habbo's history
Treat yourself with a Secret Santa gift.... of a random Wiki page for you to start exploring Habbo's history!
Happy holidays!
Celebrate with us at Habbox on the hotel, on our Forum and right here!
Join Habbox!
One of us! One of us! Click here to see the roles you could take as part of the Habbox community!


Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2004
    Location
    bristol
    Posts
    3,799
    Tokens
    0

    Latest Awards:

    Default [JavaScript] Getting a JavaScript file with AJAX and running it

    Okay, there's three parts to this script; one part which gets the correct request for the client's browser, another which makes the request and another which waits and then handles the output.

    To get the correct request we'll check if "window.XMLHttpRequest" returns true, if it does then this function is available and we'll use it; if it returns false then we'll assume the client's browser is Internet Explorer, and therefore create the correct request object for Internet Explorer.

    Code:
    if (window.XMLHttpRequest) {
    
    	var request = new XMLHttpRequest();
    
    } else {
    
    	var request = new ActiveXObject('Microsoft.XMLHTTP');
    
    }
    Now we've got the correct request object, we'll create a new GET request to retrieve our JavaScript file. Under the request is an event, which will call a function (which handles the output) once a change is detected.

    Code:
    function getJS() {
    
    	request.open('get','script.js');
    
    	request.onreadystatechange = runJS;
    
    	request.send(null);
    
    }
    The following script checks that the request is on currently on the correct state (where it has retrieved all our script), once the correct state is met we use the "eval()" function to run the response text string.

    Code:
    function runJS() {
    
    	if(request.readyState == 4) {
    
    		eval(request.responseText);
    
    	}
    }
    Bellow is a final example of the script, and I've supplied a link to a working example underneath.

    Code:
    <SCRIPT TYPE="TEXT/JAVASCRIPT">
    <!--
    
    
    
    if (window.XMLHttpRequest) {
    
    	var request = new XMLHttpRequest();
    
    } else {
    
    	var request = new ActiveXObject('Microsoft.XMLHTTP');
    
    }
    
    
    
    
    function getJS() {
    
    	request.open('get','script.js');
    
    	request.onreadystatechange = runJS;
    
    	request.send(null);
    
    }
    
    
    
    
    function runJS() {
    
    	if(request.readyState == 4) {
    
    		eval(request.responseText);
    
    	}
    }
    
    
    
    
    document.write('Get & run JS'.link('javascript:getJS();'));
    
    
    -->
    </SCRIPT>
    http://joshjh.pwp.blueyonder.co.uk/AJAXExample.htm
    kinda quit.

  2. #2
    Join Date
    Dec 2005
    Location
    XX
    Posts
    2,308
    Tokens
    2,015

    Latest Awards:

    Default

    I started learning ajax a few weeks ago


  3. #3
    Join Date
    Dec 2004
    Posts
    7,327
    Tokens
    1,276
    Habbo
    ---MAD---

    Latest Awards:

    Default

    whats it used for though?
    ---MAD---

  4. #4
    Join Date
    Aug 2004
    Location
    bristol
    Posts
    3,799
    Tokens
    0

    Latest Awards:

    Default

    AJAX? You can get/send data without the user's page reloading.
    kinda quit.

  5. #5
    Join Date
    Dec 2005
    Location
    XX
    Posts
    2,308
    Tokens
    2,015

    Latest Awards:

    Default

    Yeah saves bandwidth too, very usefull


  6. #6
    Join Date
    Aug 2004
    Location
    UK
    Posts
    11,283
    Tokens
    2,031

    Latest Awards:

    Default

    Can you realy learn ajax? since technicly its more a technique you do with javascript that something in itself.

    Also before the ajax indies come in technicaly its not ajax since it doesnt use the code asynconusly and it doesnt work with xml "/

  7. #7
    Join Date
    Aug 2004
    Location
    bristol
    Posts
    3,799
    Tokens
    0

    Latest Awards:

    Default

    Quote Originally Posted by 01101101entor
    Can you realy learn ajax? since technicly its more a technique you do with javascript that something in itself.
    He might have been referring to learning the technique.

  8. #8
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    3,285
    Tokens
    0

    Latest Awards:

    Default

    I wouldn't use AJAX as a way to save bandwidth, because I won't save much at all.



    i used to be NintendoNews. visit my blog or add me on twitter.
    need help with vista? i am a microsoft certified technology specialist in configuring windows vista and connected home integrator.. pm me for help!


    "I am the way, the truth, and the life. No one comes to the Father except through me"
    John 14:6 (NIV)


  9. #9
    Join Date
    Aug 2004
    Location
    UK
    Posts
    11,283
    Tokens
    2,031

    Latest Awards:

    Default

    Quote Originally Posted by NintendoNews
    I wouldn't use AJAX as a way to save bandwidth, because I won't save much at all.
    much at all? lets say you have a big page.
    86 kb for my useless info archive. Now u want people to vote on a fact, You can go normal, vote, then transfer another 86kb page. or u can sent a tiny ajax request, And take less than a kb to do so... thats a VERY big saveing...

  10. #10
    Join Date
    Dec 2004
    Posts
    7,327
    Tokens
    1,276
    Habbo
    ---MAD---

    Latest Awards:

    Default

    Quote Originally Posted by NintendoNews
    I wouldn't use AJAX as a way to save bandwidth, because I won't save much at all.
    Some people over do it however if you use it in the right places, its VERY helpful.

    Also read mentors post
    ---MAD---

Posting Permissions

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