Last active
December 14, 2015 09:29
-
-
Save eeroan/5065383 to your computer and use it in GitHub Desktop.
Poll untill styles are loaded
This file contains 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
var href = 'http://url.com' | |
appendStyle(href) | |
waitForStyles(loadApp) | |
function appendStyle(href) { | |
var link = document.createElement('link') | |
link.setAttribute('rel', 'stylesheet') | |
link.setAttribute('type', 'text/css') | |
link.setAttribute('href', href) | |
document.head.appendChild(link) | |
} | |
function waitForStyles(callback) { | |
var interval = setInterval(findStyles, 50) | |
function findStyles() { | |
if (styleFound()) { | |
clearInterval(interval) | |
callback() | |
} | |
} | |
function styleFound() { | |
var styleSheets = document.styleSheets | |
for (var i = 0, length = styleSheets.length; i < length; i++) { | |
var styleHref = styleSheets[i].href | |
if (styleHref && styleHref.indexOf(href)) return true | |
} | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment