0

Is there a way to stop reloading jQuery with each HTML page? For example, I've got two HTML pages both containing the following:

  <body>
      <script src="jquery"></script>

In both cases, the same jQuery is sent to the browser. IE. Jquery-1.6.min.js (at present)

Fiddler appears to show that the file is sent in both cases. This is the Fiddler display:

#  Result  Protocol  Host            URL      Body    Caching  Content-Type

1  200     HTTP      localhost:4567  /        9,101            text/html;charset=utf-8
2  200     HTTP      localhost:4567  /jquery  90,518           text/html;charset=utf-8
3  200     HTTP      localhost:4567  /namadd  8,706            text/html;charset=utf-8
4  200     HTTP      localhost:4567  /jquery  90,518           text/html;charset=utf-8

Currently I'm using the Ruby-Sinatra micro-framework to serve the data, but eventually it could be something else.

  • Item 1 from above shows an HTML page being sent.
  • Item 2 shows that HTML loading jQuery, using <script src="jquery"></script>.
  • Item 3 shows another HTML page being loaded.
  • Item 4 shows that HTML loading jQuery, using <script src="jquery"></script>
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
brianoh
  • 3,407
  • 6
  • 23
  • 23
  • 7
    Why is your script tag included in the body ? Put it in the header. – Denys Séguret Oct 09 '12 at 11:16
  • 4
    @dystroy **why ???** If you want to correct him - tell him to put it in the END of the body where form closes. it is bad to put it at the head ! the page load will be blocked.( exclude the situation where jquery is used to build the page) – Royi Namir Oct 09 '12 at 11:21
  • 2
    @dystroy It's a good idea to put it in the body, after all the content, in order to prevent delays with the rendering of the page: http://stackoverflow.com/questions/2451417/whats-pros-and-cons-putting-javascript-in-head-and-putting-just-before-the-body – skimberk1 Oct 09 '12 at 11:23
  • @RoyiNamir I'm not talking of a script using jquery but of the jquery.js addition. Have a look at js tutorials (all placing it in the head) or at [this](http://stackoverflow.com/questions/3531314/should-i-write-script-in-the-body-or-the-head-of-the-html) – Denys Séguret Oct 09 '12 at 11:25
  • 1
    @skimberk1: I note that in the linked page the hhighest voted answer says to put them in the head... – Chris Oct 09 '12 at 11:27
  • 1
    @Chris It says that it's best to put them in the head, although it isn't always possible to do so. – skimberk1 Oct 09 '12 at 11:28
  • @dystroy jquery script is no different than other scripts. Sctips should be loaded last ! (unless help to build the page). please read http://developer.yahoo.com/performance/rules.html#js_bottom – Royi Namir Oct 09 '12 at 12:10

3 Answers3

3

In general what you are talking about here is caching. With dynamic pages (IE: things like ASP.NET, php, etc.) you can generally define the cacheability of the page programatically, by adding the appropriate HTTP headers.

With static files such as this though, the caching of it is going to be up to the web server and its setup to determine. There is no easy way in the HTML for it to be cached (you might be able to do it using complicated methods) but what you want to do is find out how to configure the server to set the file to be cacheable (you really want to have it cached on the client so that it won't request it again).

Markus Unterwaditzer
  • 7,992
  • 32
  • 60
Chris
  • 27,210
  • 6
  • 71
  • 92
1

In you case you can check before loading another jquery library

Like this way

if(window.$==undefined){ //Will return false if jquery is already loaded
   load your script 
}else{
  do not load the script 
}
Soarabh
  • 2,910
  • 9
  • 39
  • 57
0

The only ways that I could find to solve this problem are :

  1. The new sessionStorage and localStorage features in browsers.

  2. ajax.googleapis.com

What I want to achieve is to download jQuery only once to use by multiple forms.

The Google solution is pretty amazing, because you only have to load it once forever unless the browser cache is cleared or similar. That saves a lot of bandwidth. Perhaps localStorage achieves the same

The sessionStorage and localStorage probably achieve similar, but I haven't tried using them yet, but I may do simply to remove reliance on a 3rd party. I mean, Google is a business, right.

brianoh
  • 3,407
  • 6
  • 23
  • 23