-2

What is best way to use/call JQUERY in webpages.

should we link to jquery website ? or make a copy in my local website.

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
                                          OR
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>

Tell the advantages as well !

Note: I don't like this question to be closed, But I wanted to restrict my website being referred to external site during loading..

logan
  • 7,946
  • 36
  • 114
  • 185
  • Thank you all.. I hope everyone who searches google about this confusion will also get clarified from your answers... There is no need to close this question ! – logan Feb 09 '14 at 11:26

5 Answers5

1

For internet applications, it's better to use a CDN:

There are a couple of benefits to using a CDN. The first is a faster experience to the user, because the jQuery library file is downloaded from the server closest to them, rather than from your servers. Often the file won’t be required at all. jQuery is so popular that the user’s browser may have already cached the library from another application that also uses jQuery. The second benefit is that none of your precious and expensive bandwidth is spent delivering jQuery to the user. For high-traffic sites, this can be a significant cost savings.

Be aware that:

When using a CDN, you must have confidence in the CDN operator. You want to be sure that the user receives the file they are supposed to and that service will always be available

Therefore, you need to choose reliable CDNs:

Google and Microsoft both provide CDN services for jQuery (and other popular JavaScript libraries) free of charge. Both companies have good experience running highly available services and are unlikely to deliberately tamper with the jQuery library

For intranet applications:

The CDN approach isn’t suitable for applications that are delivered to users within an intranet because it causes all the browsers to go to the Internet to get the jQuery library, rather than access the local server, which is generally closer and faster and has lower bandwidth costs.

Quoted from the book Pro JQuery

Khanh TO
  • 48,509
  • 13
  • 99
  • 115
1

It depends. For applications that combine all their javascripts into one file, say, application.js (like Rails does) so that the browser only loads all the javascript needed for the whole application once and never again, it's a good thing to have a local version that is compiled together with the rest of your javascript. Even in that situation there's no explicit script tag link to jquery code. it'd be available in the application.js. So a single <script src="js/application.js"></script> suffices.

Otherwise, you should go for a CDN. Basically, CDNs (e.g. http://code.jquery.com, cdnjs, Google CDN) are file servers located in different regions around the world and a piece of software which determines which place is closest to the source of the request and uses the files from that region. So while you'd always be serving from, say, Maryland to a client in Zimbabwe, a CDN could serve them with files from their servers in South Africa. They have a cache advantage too. You should read more about them.

Yaw Boakye
  • 10,352
  • 1
  • 17
  • 25
1

( Likely many answers to this on SO with a search, but yes, speaking in terms of public websites where you have no control over what network your visitors arrive from .. )

Loading external files from a CDN has benefits for bandwidth reduction but we face the issues of visitors not being able to access the file in some situations -

1) blocked access ( corporate lan et all )

2) The hosting provider not being able to serve the file

It is commonly used to try a download from the CDN and fall back to a local copy. The period of time to 'give up' is what you need to consider based on how much content relies on the script.

When calling files that load synchronously from external sources, we would always place them after content to prevent page content loading delay.

Makes sense.

Rob Sedgwick
  • 5,216
  • 4
  • 20
  • 36
  • code.jquery.com is trusted I hope ? – logan Feb 09 '14 at 10:59
  • 1
    Trusted by corp lans, aye, you would hope so. But I guess we can't code for hope : ) – Rob Sedgwick Feb 09 '14 at 11:00
  • 1
    Kept this out of the answer, but personally I just serve a local copy for most applications, If I needed to benefit from a CDN, and the bandwidth is saving £££'s , it is likely that many other static elements would also need a CDN and so use a service like 'Akami' / 'cdnify' or similar. ( keeping on the same domain as the website ) – Rob Sedgwick Feb 09 '14 at 11:08
0

Here you might find your answer :

Why should I use Google's CDN for jQuery?

Buth then again if you dnt wish fr external scripts dnt use cdn copy

Community
  • 1
  • 1
Makrand
  • 587
  • 5
  • 14
0

you can do both, like this:

<script src="//ajax.aspnetcdn.com/ajax/jquery/jquery-2.0.0.min.js"></script>
<script>window.jQuery || document.write('<script src="js/jquery-2.0.0.min.js">\x3C/script>')</script>

In this scenario it will try to load jQuery from CDN, if it fails then load a local version of jQuery.

There is an advantage of using CDN, because the jquery can be already cached in user's browser and won't require loading again. But if you require minimum amount of external depedencies — use local version. Another good advice is to include it just before the closing body tag, so that it doesn't block rendering of the page.

good reference with different implementation examples

vitkon
  • 1,058
  • 8
  • 13