3 ms·
I think this is the correct way to do this now: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> (func
by samwillis 10y ago
I think this is the correct way to do this now:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
(function(){ if(!window.jQuery) {
var s = document.createElement("script"),
s0 = document.getElementsByTagName('script')[0];
s.src = "../lib/js/jquery-2.1.1.min.js";
s.async = false;
s0.parentNode.insertBefore(s, s0);
}})();
</script>
EDIT:
Saving a few bytes although a little less readable:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>(function(w,d,x,t,p,s,z){if(!w[t]){s=d.createElement(x);s.src=p;
s.async=false;z=d.getElementsByTagName(x)[0];z.parentNode.insertBefore(s,z);}
})(window,document,'script','jQuery','../lib/js/jquery-2.1.1.min.js');</script>
- Globz 10y agoYeah I believe this is the best approach, in my case I would have never seen the warning from the console, the only way I would have known that this is no longer valid in Chrome is for Google to stop serving my version of JQuery + my fail over failing on me for using a bad practice. Thank you!
- MatthewPhillips 10y agoSetting async to false doesn't make it synchronous. Try adding another <script> after this one that uses $ and see what I mean. document.write() is the only way to synchronously inject a script.