Skip to content

Instantly share code, notes, and snippets.

@eeroan
Last active December 14, 2015 09:29
Show Gist options
  • Save eeroan/5065383 to your computer and use it in GitHub Desktop.
Save eeroan/5065383 to your computer and use it in GitHub Desktop.
Poll untill styles are loaded
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