earth tech

[software and hardware technology originating from or otherwise pertinent to planet earth]

use these buttons to share this blog on portal sites:

Add to Google

Sunday, November 8, 2009

 

AJAX Offline: referencing the window.location

http://www.javascriptkit.com/jsref/ajax.shtml

The javascriptkit.com site makes some interesting points about AJAX and the implementation of the responseXML property of the XMLHttpRequest object.

"If you're running an Ajax request offline locally on your PC, a value of 0 is returned by status in most browsers regardless of the actual request status, even if it was successful. To run and test Ajax requests locally, it should overlook the status code being 200, and merely rely on a readyState value of 4. The following example takes into account the request possibly being run offline and to enable it:"

var myrequest = new ajaxRequest()
myrequest.onreadystatechange=function() {
  if ( myrequest.readyState == 4 ) { //if request has completed
    if ( myrequest.status == 200 || 
      window.location.href.indexOf("http") == -1 ) {

      /* if request was successful 
         (versus "page not found" etc) 
         or if run offline (rely on readyState of
         4 alone then) */

      // ... do something with returned data ...

  }
 }
}

From the example, it is clear that the test

window.location.href.indexOf("http") == -1

can be generally useful for other things - like finding the protocol string from the URL of the current document. In theory, at least, this should tell you what protocol was used to load the content of window.

Labels: , , ,

Archives

2006/12   2007/01   2007/05   2007/06   2007/07   2007/08   2007/10   2008/01   2008/02   2008/03   2008/05   2008/11   2008/12   2009/01   2009/02   2009/03   2009/04   2009/05   2009/07   2009/09   2009/10   2009/11   2009/12   2010/01  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]