4 Replies - 879 Views - Last Post: 08 June 2011 - 04:28 AM

#1 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Peculiarities regarding loading ajax-enabled HTML files

Posted 08 June 2011 - 04:12 AM

Hi, I just asked a kind of similar question a few days back, though this time my problem is different. With normal HTML files, all I require to do is refer the associated CSS/Javascript files in it through proper addressing, and then it can be loaded in the browser either by double-clicking the HTML file, or by entering the address in the address bar, like file:///C:/Documents%20and%20Settings/tatai/Desktop/Books%20and%20Codes/jQuerycodes/ch3/cool.html. With HTML files requiring files to be loaded by Ajax, I could not do it because Ajax requires a server, so I had to upload them into WAMP (desktop folders hardly qualify as server!), and after that too, double-clicking that file in WAMP to load it in browser didn't allow Ajax calls to be executed, rather I have to type in localhost/filename.html in the address bar, then only it works as expected. However, if I write the Ajax part of the code in jQuery instead of vanilla Javascript, for example using $.getJSON, I found that it works either way, both by double-clicking the file or typing in the relative url in the address bar, and that too both from desktop folder and WAMP folder. Can somebody explain this difference between using jQuery and plain Javascript?

This post has been edited by cupidvogel: 08 June 2011 - 04:14 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Peculiarities regarding loading ajax-enabled HTML files

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: Peculiarities regarding loading ajax-enabled HTML files

Posted 08 June 2011 - 04:15 AM

I guess jQuery is smart enough to check whether the URL contained "file://" and then checks if the status is 0 (the return value for the file: protocol, as opposed to 200 for http:)


PS. see comment here.

This post has been edited by Dormilich: 08 June 2011 - 04:17 AM

Was This Post Helpful? 1
  • +
  • -

#3 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Peculiarities regarding loading ajax-enabled HTML files

Posted 08 June 2011 - 04:20 AM

Yeah, that is possible, of course. But the double-clicking problem remains. When the Ajax part is written in Javascript, I can understand why doesn't double-clicking allow Ajax calls to be executed, for the url of the page in the browser becomes same as the relative address of the file, i.e file:///C:/wamp/www/dictionary.html, whereas it should be http://localhost/ajax.html. So why does double-clicking allow Ajax calls to be executed with jQuery too, where too the url becomes same as the file's relative address?

This post has been edited by cupidvogel: 08 June 2011 - 04:21 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: Peculiarities regarding loading ajax-enabled HTML files

Posted 08 June 2011 - 04:26 AM

AJAX as such is not dependent on a server, only some properties of the XMLHttpRequest object are. that is, if your "server" does not send HTTP status codes, then the XHR object will use its default values.

AJAX uses the browser to make those communication calls, after all. you can see the XMLHttpRequest object as an Interface for the Client-Server-Communication (and that happens even for file:)
Was This Post Helpful? 1
  • +
  • -

#5 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Peculiarities regarding loading ajax-enabled HTML files

Posted 08 June 2011 - 04:28 AM

Gotcha.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1