What better - Jquery file from my SERVER or from URL? [duplicate] - javascript

This question already has answers here:
Where do you include the jQuery library from? Google JSAPI? CDN?
(16 answers)
Closed 7 years ago.
What better - Jquery file from my SERVER or from URL?
<script src="jquery.js"></script>
or
<script src='http://code.jquery.com/jquery-1.7.1.min.js'></script>

All too often, I find code similar to this when inspecting the source for public websites that use jQuery:
<script type="text/javascript" src="jQuery.min.js"></script>
If you’re doing this on a public facing website, you are doing it wrong.
Instead, I urge you to use the Google Hosted Libraries content delivery network to serve jQuery to your users directly from Google’s network of data centers. Doing so has several advantages over hosting jQuery on your server(s): decreased latency, increased parallelism, and better caching.
Better use this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

Related

Web design practices to minimize browser refresh [duplicate]

This question already has answers here:
How can I force clients to refresh JavaScript files?
(30 answers)
Closed 2 years ago.
The design of our website is such that whenever we make some changes in CSS & JavaScript files we require the user to clear the browser cache and get the latest version of these files from the server. This is highly undesirable from the user perspective and I don't see well designed websites out there having this issue.
Are there any website design best practices that can be followed to minimize this issue? Any pointers would be appreciated.
Check out more on cache-busting. When you make changes to your css and javascript, you can append a version type to force the browser to reload the document.
Example:
<script src="myjavascript.js?v=1.1"></script>
<script src="myjavascript.js?v=1.2"></script>
etc : )

How to Use Multiple CDN for Library Scripts? [duplicate]

This question already has answers here:
Best way to use Google's hosted jQuery, but fall back to my hosted library on Google fail
(23 answers)
Closed 5 years ago.
I want to use both "Google Hosted Libraries" and "Microsoft Ajax Content Delivery Network" for library scripts like jQuery, so that even if one fails to load (e.g. Google), another one is loaded, and the webpage is rendered correctly.
Since Google is banned in China and in some other regions, it is necessary to add backup sources like Microsoft/CDNJS or local files.
Please note that my jQuery is at the bottom of the page, not in the head.
Could you please give me a reliable solution? It'd be great if it's loaded in the following order:
Google>CDNJS/MICROSOFT>Local File
This question is not duplicate! I didn't find any reliable/specific solution for this issue.
Insert the following javascript right after your initial script tag.
<script src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
(window.jQuery)||document.write('<script type="text/javascript" src="/scripts/jquery-2.2.4.min.js"><\/script>');//]]>
</script>
Explanation:
Try load the jQuery script from a cdn
If that fails load a fallback script from js
Note: replace the url with your preferred fallback url

What is the purpose of the integrity attribute in HTML? [duplicate]

This question already has answers here:
What are the integrity and crossorigin attributes?
(3 answers)
Closed 7 years ago.
I was on bootstrap's site, and I recently noticed that their CDN links contained an integrity attribute with an SHA-384 key.
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
I assume that is meant to be a way to verify the script source, but moreso I was wondering how it's used and if this is part of any spec?
Furthermore, does this only work with script src's or can it work with any non-same-origin source?
check this :
https://developer.mozilla.org/en/docs/Web/HTML/Element/script
Using Content Delivery Networks (CDNs) to host files such as scripts and stylesheets that are shared among multiple sites can improve site performance and conserve bandwidth. However, using CDNs also comes with a risk, in that if an attacker gains control of a CDN, the attacker can inject arbitrary malicious content into files on the CDN (or replace the files completely) and thus can also potentially attack all sites that fetch files from that CDN.
The Subresource Integrity feature enables you to mitigate the risk of attacks such as this, by ensuring that the files your Web application or Web document fetches (from a CDN or anywhere) have been delivered without a third-party having injected any additional content into those files — and without any other changes of any kind at all having been made to those files.
Read more here :
https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity

Difference between using local JS and JS from CDN [duplicate]

This question already has answers here:
What are the advantages and disadvantages of using a content delivery network (CDN)? [closed]
(4 answers)
Closed 7 years ago.
I am trying to write a JavaScript script to populate HTML using jQuery and Bootstrap. In many tutorials on the internet, tutors mention to using files from a Content Delivery Network(CDN) instead of calling those files locally.
But I am unable to foresee any advantage making a call to js or CSS over a network, instead of loading it locally, which should prove good enough.
I am eager to know, what is the difference in terms of network and machine resources as well as the load on a page.
Pros using CDN:
* cache for the library in the client side.
Cons using CDN:
* If the cdn's site is down, your site will not get the files.
* Cdn file changes frequently and therefore your production site will work with the newest files that you didn't checked them(this can be very dangerous).
Now you can think if this suitable for your site.

Should I be concerned if my website is linked to an external Style Sheet [duplicate]

This question already has answers here:
Why should I use Google's CDN for jQuery?
(7 answers)
Closed 7 years ago.
At some point in my website I needed a Timer so I looked for a free jQuery Countdown Timer and found this one : Example
After integrating the model to my page inside my IDE (VS2010) i payed attention that some CSS and JS files are not stored locally in my project folder, but they are still Linked to an External sources, and that had me thinking : Am i suppose to find a way to download these file locally than use them, or should i use them the way they are ? and should i be concerned if they may change or desperate at some point in the future ? what are the best practices in case ?
Here is an example of the HTML code :
....
....
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<script type="text/javascript" src="inc/TimeCircles.js"></script>
....
....
Those are CDNs ( http://it.wikipedia.org/wiki/Content_Delivery_Network ) meaning that they're hosted by someone for all of us to use, so you're pretty much guaranteed it will stay there. The main advantage of using CDNs is that the user will probably have visited another site that uses the same resource and this means that said resource is already cached in the user's computer, leading to a faster loading time for your site.
You should never rely on external sources for critical files unless you're using a dependable CDN. In this case you're using the most common CDN sources for Bootstrap and jQuery, so you're all set.
I assume that you've downloaded the timer files and are hosting those locally. Your reference to them confused me, so I've updated this answer.

Categories