SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question different ajax objects?

    Can someone explain to me the difference in the book and the sample code? I am not an expert in javascript, but I am an experienced programmer in C++ (Borland) and other programming languages (mainly CVI which does not support OOP).
    The book uses try and catch statements to init the XMLHTTPRequest object, but the sample code uses what looks like (to me) an array of functions. Can someone please explain the sample code? It is probably straightforward to a javascript programmer, and I can see what is happeneing, but I have never seen the square brackets construct before.

    Sample code:

    this.init = function() {
    var i = 0;
    var reqTry = [
    function() { return new XMLHttpRequest(); },
    function() { return new ActiveXObject('Msxml2.XMLHTTP') },
    function() { return new ActiveXObject('Microsoft.XMLHTTP' )} ];

    while (!this.req && (i < reqTry.length)) {
    try {
    this.req = reqTry[i++]();
    }
    catch(e) {}
    }
    return true;
    };

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [] is an array literal, for example
    Code:
    var aMyArray = ["testing", "array", "literal"];
    The code you provide will iterate through the array and try to instanciate the XMLHttpRequest (XHR) object. First it will try with the native JavaScript XHR for modern browsers (IE 7, Firefox, Opera, Safari etc). If that fails (therefore the try and catch) it will try to instanciate a XHR ActiveX object used in older IE versions. There are different versions of this ActiveX object, therefore it will first try with the newer one.

  3. #3
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Wow! Such a quick response. Thanks for that. I am totally enlightened.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nice code bellow
    cheers

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Third Stone From The Sun
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In javascript, function pointers are functions.

    Thusly, you can say:

    var x = function() { alert ( 9 ); };

    x();

    and then put in an array (as already explained).
    exes = [
    function() {alert(1);},
    function() {alert(2);
    }];

    exes[0]();
    \\

    Function pointers are very useful feature of JS!
    My outdated site is down for a while now.


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
  •