Skip to content

Instantly share code, notes, and snippets.

@baldwicc
Created October 16, 2013 10:02
Show Gist options
  • Save baldwicc/7005476 to your computer and use it in GitHub Desktop.
Save baldwicc/7005476 to your computer and use it in GitHub Desktop.
Adds script files <head> with a simple callback once loaded. (Note: Callback fired on 'load' event - not necessarily before / after actual execution)
var _qut = _qut || {};
_qut.addscript = function(src, fn) {
var head = $$("head")[0],
script = new Element('script', {
type: 'text/javascript',
async: true,
defer: true
}),
callback = function() {
typeof(fn) === "function" && fn(src);
};
// IE8
if (script.readyState) {
script.onreadystatechange = function() {
if (script.readyState === "loaded" || script.readyState === "complete") {
script.onreadystatechange = null;
callback();
}
};
}
// Others
else {
script.onload = callback;
}
// Load dat script
script.src = src;
head.appendChild(script);
};
<script>
var _qut = _qut || {};
_qut.addscript = function(e, t) {
var a = $$("head")[0], n = new Element("script", {
type: "text/javascript",
async: !0,
defer: !0
}), d = function() {
"function" == typeof t && t(e);
};
n.readyState ? n.onreadystatechange = function() {
("loaded" === n.readyState || "complete" === n.readyState) && (n.onreadystatechange = null,
d());
} : n.onload = d, n.src = e, a.appendChild(n);
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment