Created
September 17, 2012 00:56
-
-
Save joelclermont/3735026 to your computer and use it in GitHub Desktop.
using yepnope for twitter js and css with local fallback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cssLoaded(href) { | |
var cssFound = false; | |
for (var i = 0; i < document.styleSheets.length; i++) { | |
sheet = document.styleSheets[i]; | |
if (sheet['href'].indexOf(href) >= 0 && sheet['cssRules'].length > 0) { | |
cssFound = true; | |
} | |
}; | |
return cssFound; | |
} | |
yepnope([{ | |
load: '//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.1.min.js', | |
complete: function () { | |
if (!window.jQuery) { | |
alert('local jquery'); | |
yepnope('js/jquery-1.8.1.min.js'); | |
} | |
} | |
}, { | |
load: '//netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/js/bootstrap.min.js', | |
complete: function () { | |
if (!$.fn.button) { // just picked a random element from the bootstrap to test | |
alert('local twitter js'); | |
yepnope('js/bootstrap.min.js'); | |
} | |
} | |
}, { | |
load: '//netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/css/bootstrap-combined.min.css' | |
}, { | |
test: cssLoaded('bootstrap-combined.min.css'), | |
nope: 'css/bootstrap.min.css' | |
}]); |
Just to clarify, I am using the css plugin to allow the complete function to only fire after the css file is loaded.
+1
Couldn't get the css complete to work predictably, so I moved it to a second test. This works 100% predictably now.
woot!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My tests for checking the twitter bootstrap js seems a bit arbitrary and I'm not sure if there's a better way to detect if the stylesheet loaded. Ideas?