Page 3 of 3 FirstFirst 123
Results 21 to 23 of 23

Thread: Ajax

  1. #21
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Quote Originally Posted by Twey View Post
    This line looks like it lost a fight with Packer — although, strangely, it's one of only a few places where the space-after-control-flow-keyword convention has been observed. Well... once.
    Not with JSBeautifier.
    Jeremy | jfein.net

  2. #22
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,475
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, Twey. Incorporating most of your suggestions, here is the new version. I hope that what I've done with ICEBrowser will work as written - it should if ICEBrowser acts like others in regards to functions. You still haven't gotten back to me on what you envision as a good basic replacement for innerHTML:

    Code:
    function loadXmlHttp(url, id) {
     var f = this;
     if(loadXmlHttp.xmlHttp !== null){
      f.xmlHttp = loadXmlHttp.xmlHttp();
      f.el = document.getElementById(id);
      f.xmlHttp.open("GET", url, true);
      f.xmlHttp.onreadystatechange = function(){f.stateChanged();};
      f.xmlHttp.send(null);
     }
     else alert('Your browser does not support AJAX!'); // substitute your desired request object unsupported code here
    }
    
    loadXmlHttp.xmlHttp = null; loadXmlHttp.re = /^http/.test(window.location.href);
    /*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it
    /*@if(@_jscript_version >= 5) // prevents errors in old browsers that barf on try/catch & problems in IE if Active X disabled
    try {loadXmlHttp.ie = window.ActiveXObject}catch(e){loadXmlHttp.ie = false;};
    @end @*/
    if (window.XMLHttpRequest && (!loadXmlHttp.ie || loadXmlHttp.re))
     loadXmlHttp.xmlHttp = function(){return new XMLHttpRequest();}; // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
    else if (/(object)|(function)/.test(typeof createRequest))
     loadXmlHttp.xmlHttp = createRequest; // ICEBrowser, perhaps others
    else {
     loadXmlHttp.xmlHttp = null;
    // Internet Explorer 5 to 6, includes IE 7+ when local //
    /*@if(@_jscript_version >= 5)
    try{loadXmlHttp.xmlHttp = function(){return new ActiveXObject("Msxml2.XMLHTTP");};}
     catch(e){try{loadXmlHttp.xmlHttp = function(){return new ActiveXObject("Microsoft.XMLHTTP");};}catch(e){loadXmlHttp.xmlHttp = null;}}
    @end @*/
    }
    
    loadXmlHttp.prototype.stateChanged = function(){
     if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !loadXmlHttp.re))
      this.el.innerHTML = this.xmlHttp.responseText;
    }
    The usage is still the same:

    Code:
    new loadXmlHttp('requested_url', 'target_element_id')
    But now most of the 'heavy lifting' is only done once. And I believe I corrected an error in the logic (highlighted). I haven't tested it (the whole thing) much though, so it may need some tweaking.

    Oh, and this reminds me that I still hadn't responded to your question about:

    Code:
    var f = this;
    We need a variable for the instance, here:

    Code:
      f.xmlHttp.onreadystatechange = function(){f.stateChanged();};
    I used f because I couldn't think of anything else, and it is after all a function. And since it is required anyway, may as well use it as shorthand in the rest of the code.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #23
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,475
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If you are reading this, please also refer to the (my) previous post in this thread, unless you were directed here in single post view. Anyways, the current code I'm using is a bit reduced over that in my previous post, here it is:

    Code:
    function loadXmlHttp(url, id) {
     var f = this;
     if (loadXmlHttp.xmlHttp){
      f.xmlHttp = loadXmlHttp.xmlHttp();
      f.el = document.getElementById(id);
      f.xmlHttp.open("GET", url, true);
      f.xmlHttp.onreadystatechange = function(){f.stateChanged();};
      f.xmlHttp.send(null);
     }
     else alert('Your browser does not support AJAX!'); // substitute your desired request object unsupported code here
    }
    
    loadXmlHttp.xmlHttp = null; loadXmlHttp.re = /^http/.test(window.location.href);
    /*@cc_on @*/ // used here and below, limits try/catch to those IE browsers that both benefit from and support it
    /*@if(@_jscript_version >= 5) // prevents errors in old browsers that barf on try/catch & problems in IE if Active X disabled
    try {loadXmlHttp.ie = window.ActiveXObject}catch(e){};
    @end @*/
    if (window.XMLHttpRequest && (!loadXmlHttp.ie || loadXmlHttp.re))
     loadXmlHttp.xmlHttp = function(){return new XMLHttpRequest();}; // Firefox, Opera 8.0+, Safari, others, IE 7+ when live - this is the standard method
    else if (/(object)|(function)/.test(typeof createRequest))
     loadXmlHttp.xmlHttp = createRequest; // ICEBrowser, perhaps others
    else {
     loadXmlHttp.xmlHttp = null;
    // Internet Explorer 5 to 6, includes IE 7+ when local //
    /*@if(@_jscript_version >= 5)
    try{loadXmlHttp.xmlHttp = function(){return new ActiveXObject("Msxml2.XMLHTTP");};}
     catch(e){try{loadXmlHttp.xmlHttp = function(){return new ActiveXObject("Microsoft.XMLHTTP");};}catch(e){}}
    @end @*/
    }
    
    loadXmlHttp.prototype.stateChanged = function(){
     if (this.xmlHttp.readyState == 4 && (this.xmlHttp.status == 200 || !loadXmlHttp.re))
      this.el.innerHTML = this.xmlHttp.responseText;
    }
    Usage:

    Code:
    new loadXmlHttp('requested_url', 'target_element_id')
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Tags for this Thread

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
  •