Skip to content

Instantly share code, notes, and snippets.

@phiggins42
Created January 11, 2011 13:42
Show Gist options
  • Save phiggins42/774417 to your computer and use it in GitHub Desktop.
Save phiggins42/774417 to your computer and use it in GitHub Desktop.
(function(d){
window.noteval = function(/* String */code){
// summary: Execute some javascript.
if(!code) return;
var e = d.createElement("script"),
// jump through the cross-browser hoops:
how = "text" in e ? "text" :
"textContent" in e ? "textContent" :
"innerHTML" in e ? "innerHTML" :
"appendChild"
;
if(how == "appendChild"){
e[how](d.createTextNode(code));
}else{
e[how] = code;
}
return d.getElementsByTagName("head")[0].appendChild(e);
};
})(document);
@theophani
Copy link

Why not: d.head.appendChild(e) ?

@phiggins42
Copy link
Author

you'd have to do var head = d.head || d.getElementsByTagName("head")[0]; for x-browser anyway.

@phiggins42
Copy link
Author

also this does little more than:

eval("(" + code + ")");

@theophani
Copy link

Nevermind, I answered my own question. Because document.head is not available everywhere. I use document.body.appendChild() and always found it distasteful. This is nice.

@phiggins42
Copy link
Author

@theophani
Copy link

Regarding: "also this does little more than: eval("(" + code + ")");"

Thank you. Learned.

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