I've seen pages that instruct or ask how to fallback from Zepto to jQuery (especially for IE), as here on SO and here on Zepto.js official page.
I've also seen examples on how to fallback from Google-hosted jQuery to a local site jQuery, as in Modernizr.load doc page.
My question is, how do I put the two things together? Possibly also without using Modernizr.load, just using proper <script>
blocks?
Here's what I came up with, but it seems IE never finds the Google hosted version. Also, I'm not sure the Zepto = jQuery
assignment works properly.
<script>
document.write('<script src=' +
('__proto__' in {} ?
'js/vendor/zepto.min' :
'https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min') +
'.js><\/script>');
</script>
<script>
if (window.jQuery) {
window.Zepto = window.jQuery; /* let jQuery alias Zepto */
}
else
{ /* here jQuery could be rightly undefined because Zepto is loaded,
so this could be wrong. */
document.write('<script src=' +
'js/vendor/jquery-1.8.0.min' +
'.js><\/script>');
}
</script>
<script>
if (window.jQuery) {
window.Zepto = window.jQuery; /* let jQuery alias Zepto */
}
else
{
/* same problem as before */
console.error('Zepto nor jQuery available!');
}
</script>
Is there a better way? TA
Edit
After @Ashfame answer, this is what I've used:
<!-- Load local Zepto.js or (as a fallback) jQuery from Google CDN or (as a fallback) local jQuery -->
<script>
document.write('<script src="' + ('__proto__' in {} ?
'js/vendor/zepto' :
'http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery') +
'.min.js"><\/script>')
</script>
<script>
window.Zepto || window.jQuery || document.write('<script src="js/vendor/jquery-1.8.0.min.js"><\/script>');
</script>
I could not use the protocol-less/scheme-less URL of Google CDN as for some reason it didn't work on my local IE9 (it waits a lot, then always falls back to local).
I did not aliased anymore Zepto
with jQuery
: just used $
in JS code.
I don't seem to experience any problem related to jQuery loading out of order w.r.t. dependent code.
//ajax.googleapis.com/...
, that is without protocol prefix – FogyModernizr.load
, but somehow it seemed a little bit too much for the purpose. I might be wrong of course. – Fogy