Skip to content

Instantly share code, notes, and snippets.

@clarkdave
Created October 28, 2012 03:14
Show Gist options
  • Save clarkdave/3967295 to your computer and use it in GitHub Desktop.
Save clarkdave/3967295 to your computer and use it in GitHub Desktop.
widget stuff
function loadScript(url, fn) {
var script = document.createElement('script');
script.async = true;
script.src = '//' + url;
var entry = document.getElementsByTagName('script')[0];
entry.parentNode.insertBefore(script, entry);
script.onload = script.onreadystatechange = function() {
var rdyState = script.readyState;
if (!rdyState || /complete|loaded/.test(script.readyState)) {
fn();
script.onload = null;
script.onreadystatechange = null;
}
};
}
/**
* Load a stylesheet from the provided url
*/
function loadStylesheet(url) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = ('https:' == document.location.protocol ? 'https' : 'http') + '://' + url;
var entry = document.getElementsByTagName('script')[0];
entry.parentNode.insertBefore(link, entry);
}
/**
* Check at frequent intervals to determine if the browser has loaded and
* applied our CSS styles. Run the fn callback when this has occured.
*/
function whenCSSReady(fn) {
var testElem = document.createElement('span');
testElem.id = 'prizgo-css-ready';
testElem.style = 'color: #fff';
var entry = document.getElementsByTagName('script')[0];
entry.parentNode.insertBefore(testElem, entry);
(function poll() {
var node = document.getElementById('css-ready');
var value;
if (window.getComputedStyle) {
value = document.defaultView.getComputedStyle(testElem, null).getPropertyValue('color');
} else if (node.currentStyle) {
value = node.currentStyle.color;
}
if (value && value === 'rgb(186, 218, 85)' || value.toLowerCase() === '#bada55') {
fn();
} else {
setTimeout(poll, 500);
}
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment