Skip to content

Instantly share code, notes, and snippets.

@wouterj
Created July 5, 2012 09:41
Show Gist options
  • Save wouterj/3052657 to your computer and use it in GitHub Desktop.
Save wouterj/3052657 to your computer and use it in GitHub Desktop.
My YepNope jQuery version split solution
<!doctype html>
<meta charset=utf-8>
<title>jQie demo</title>
<!-- lots of code -->
<script src=js/libs/jqie.js></script>
<script>
// only load the jQuery source
jQie();
// load scripts-1.9.js if jQuery1.9 is included and scripts-2.0.js if jQuery2.0 is included
jQie('js/scripts-1.9.js', 'js/scripts-2.0.js');
// load multiple files when jQuery1.9 is included
// e.g. these files can be plugins comptable for jQuery1.9 and jQuery2.0
// NOTE: this can be done with jQuery2.0 files too
jQie([
'js/scripts-1.9.js',
'js/1.9-more-code.js'
], 'js/scripts-2.0.js');
// give a fallback if Google CDN is out
jQie(null, null, 'js/libs/jquery-1.9.min.js', 'js/libs/jquery-2.0.min.js');
// combine the arguments
jQie('js/scripts-1.9.js', 'js/scripts-2.0.js', 'js/libs/jquery-1.9.min.js', 'js/libs/jquery-2.0.min.js');
</script>
/**
* Check the browser, code from jQuery1.7.2 source
* ===============================================
* Producing a browser var with the name of the browser and the
* version of the browser
*/
var userAgent=navigator.userAgent,browserMatch,browser={},rwebkit=/(webkit)[ \/]([\w.]+)/,ropera=/(opera)(?:.*version)?[ \/]([\w.]+)/,rmsie=/(msie) ([\w.]+)/,rmozilla=/(mozilla)(?:.*? rv:([\w.]+))?/;var uaMatch=function(b){b=b.toLowerCase();var a=rwebkit.exec(b)||ropera.exec(b)||rmsie.exec(b)||b.indexOf("compatible")<0&&rmozilla.exec(b)||[];return{browser:a[1]||"",version:a[2]||"0"}};var browserMatch=uaMatch(userAgent);if(browserMatch.browser){browser[browserMatch.browser]=true;browser.version=browserMatch.version};
/**
* Yepnope.js
* ==========
* Creates a yepnope function for asynchronous conditional resource loading
*/
(function(l,G,e){var F=G.documentElement,o=l.setTimeout,C=G.getElementsByTagName("script")[0],B={}.toString,f=[],c=0,g=function(){},t=("MozAppearance" in F.style),n=t&&!!G.createRange().compareNode,v=n?F:C.parentNode,E=l.opera&&B.call(l.opera)=="[object Opera]",A=!!G.attachEvent&&!E,j=t?"object":A?"script":"img",w=A?"script":j,q=Array.isArray||function(H){return B.call(H)=="[object Array]"},p=function(H){return Object(H)===H},r=function(H){return typeof H=="string"},b=function(H){return B.call(H)=="[object Function]"},i=[],z={},D={timeout:function(I,H){if(H.length){I.timeout=H[0]}return I}},d,m;function s(H){return(!H||H=="loaded"||H=="complete"||H=="uninitialized")}function h(H,I,P,N,J,M){var O=G.createElement("script"),K,L;N=N||m.errorTimeout;O.src=H;for(L in P){O.setAttribute(L,P[L])}I=M?u:(I||g);O.onreadystatechange=O.onload=function(){if(!K&&s(O.readyState)){K=1;I();O.onload=O.onreadystatechange=null}};o(function(){if(!K){K=1;I(1)}},N);J?O.onload():C.parentNode.insertBefore(O,C)}function a(H,I,P,N,J,M){var O=G.createElement("link"),K,L;N=N||m.errorTimeout;I=M?u:(I||g);O.href=H;O.rel="stylesheet";O.type="text/css";for(L in P){O.setAttribute(L,P[L])}if(!J){C.parentNode.insertBefore(O,C);o(I,0)}}function u(){var H=f.shift();c=1;if(H){if(H.t){o(function(){(H.t=="c"?m.injectCss:m.injectJs)(H.s,0,H.a,H.x,H.e,1)},0)}else{H();u()}}else{c=0}}function y(J,H,Q,I,O,S,R){R=R||m.errorTimeout;var P=G.createElement(J),M=0,N=0,L={t:Q,s:H,e:O,a:S,x:R};if(z[H]===1){N=1;z[H]=[]}function K(U){if(!M&&s(P.readyState)){L.r=M=1;!c&&u();P.onload=P.onreadystatechange=null;if(U){if(J!="img"){o(function(){v.removeChild(P)},50)}for(var T in z[H]){if(z[H].hasOwnProperty(T)){z[H][T].onload()}}}}}if(J=="object"){P.data=H;P.setAttribute("type","text/css")}else{P.src=H;P.type=J}P.width=P.height="0";P.onerror=P.onload=P.onreadystatechange=function(){K.call(this,N)};f.splice(I,0,L);if(J!="img"){if(N||z[H]===2){v.insertBefore(P,n?null:C);o(K,R)}else{z[H].push(P)}}}function k(L,J,I,H,K){c=0;J=J||"j";if(r(L)){y(J=="c"?w:j,L,J,this["i"]++,I,H,K)}else{f.splice(this["i"]++,0,L);f.length==1&&u()}return this}function x(){var H=m;H.loader={load:k,i:0};return H}m=function(L){var K,M,J=this["yepnope"]["loader"];function H(P){var S=P.split("!"),X=i.length,R=S.pop(),U=S.length,V={url:R,origUrl:R,prefixes:S},Q,T,W;for(T=0;T<U;T++){W=S[T].split("=");Q=D[W.shift()];if(Q){V=Q(V,W)}}for(T=0;T<X;T++){V=i[T](V)}return V}function O(Q){var P=Q.split("?")[0];return P.substr(P.lastIndexOf(".")+1)}function I(Q,W,S,R,P){var U=H(Q),T=U.autoCallback,V=O(U.url);if(U.bypass){return}if(W){W=b(W)?W:W[Q]||W[R]||W[(Q.split("/").pop().split("?")[0])]}if(U.instead){return U.instead(Q,W,S,R,P)}else{if(z[U.url]){U.noexec=true}else{z[U.url]=1}S.load(U.url,((U.forceCSS||(!U.forceJS&&"css"==O(U.url))))?"c":e,U.noexec,U.attrs,U.timeout);if(b(W)||b(T)){S.load(function(){x();W&&W(U.origUrl,P,R);T&&T(U.origUrl,P,R);z[U.url]=2})}}}function N(R,P){var X=!!R.test,Y=X?R.yep:R.nope,T=R.load||R.both,Z=R.callback||g,Q=Z,S=R.complete||g,W,V;function U(aa,ab){if(!aa){!ab&&S()}else{if(r(aa)){if(!ab){Z=function(){var ac=[].slice.call(arguments);Q.apply(this,ac);S()}}I(aa,Z,P,0,X)}else{if(p(aa)){W=(function(){var ad=0,ac;for(ac in aa){if(aa.hasOwnProperty(ac)){ad++}}return ad})();for(V in aa){if(aa.hasOwnProperty(V)){if(!ab&&!(--W)){if(!b(Z)){Z[V]=(function(ac){return function(){var ad=[].slice.call(arguments);ac&&ac.apply(this,ad);S()}})(Q[V])}else{Z=function(){var ac=[].slice.call(arguments);Q.apply(this,ac);S()}}}I(aa[V],Z,P,V,X)}}}}}}U(Y,!!T);T&&U(T)}if(r(L)){I(L,0,J,0)}else{if(q(L)){for(K=0;K<L.length;K++){M=L[K];if(r(M)){I(M,0,J,0)}else{if(q(M)){m(M)}else{if(p(M)){N(M,J)}}}}}else{if(p(L)){N(L,J)}}}};m.addPrefix=function(H,I){D[H]=I};m.addFilter=function(H){i.push(H)};m.errorTimeout=10000;if(G.readyState==null&&G.addEventListener){G.readyState="loading";G.addEventListener("DOMContentLoaded",d=function(){G.removeEventListener("DOMContentLoaded",d,0);G.readyState="complete"},0)}l.yepnope=x();l.yepnope["executeStack"]=u;l.yepnope["injectJs"]=h;l.yepnope["injectCss"]=a})(this,document);
/**
* jQIE
* ====
* Fires a yepnope function to determine if the browser is IE, if so
* we load jQuery1.9.0 otherwise jQuery2.0.0
* please note that this won't work today, because jQuery1.9.0 isn't
* released, expect jQuery1.9.0 begin 2013.
*/
var jQie = function(files19, files20, fallback19, fallback20) {
files19 = ['http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js', files19];
files20 = ['http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js', files20];
yepnope({
test : browser.msie && (browser.version < 9),
yep : files19,
nope : files20,
callback : function(url, result) {
window.jQuery || (true === result
? (undefined !== fallback19
? document.write(unescape('%3Cscript src="' + fallback19 + '"%3E%3C/script%3E'))
: ''
)
: (undefined !== fallback19
? document.write(unescape('%3Cscript src="' + fallback20 + '"%3E%3C/script%3E'))
: ''
)
);
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment