Skip to content

Instantly share code, notes, and snippets.

@joelclermont
Created September 17, 2012 00:56
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save joelclermont/3735026 to your computer and use it in GitHub Desktop.
Save joelclermont/3735026 to your computer and use it in GitHub Desktop.
using yepnope for twitter js and css with local fallback
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'
}]);
@joelclermont
Copy link
Author

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?

@joelclermont
Copy link
Author

Just to clarify, I am using the css plugin to allow the complete function to only fire after the css file is loaded.

@jdorfman
Copy link

+1

@joelclermont
Copy link
Author

Couldn't get the css complete to work predictably, so I moved it to a second test. This works 100% predictably now.

@jdorfman
Copy link

woot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment