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>
7
  • 7
    Why is your script tag included in the body ? Put it in the header. Commented Oct 9, 2012 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) Commented Oct 9, 2012 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: stackoverflow.com/questions/2451417/… Commented Oct 9, 2012 at 11:23
  • 1
    @skimberk1: I note that in the linked page the hhighest voted answer says to put them in the head... Commented Oct 9, 2012 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. Commented Oct 9, 2012 at 11:28

3 Answers 3

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).

Sign up to request clarification or add additional context in comments.

Comments

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 
}

2 Comments

$ can be declared not by jquery. this is not accurate .
please use if (jQuery) { // jQuery is loaded } else { // jQuery is not loaded }
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.