Created
January 11, 2011 13:42
-
-
Save phiggins42/774417 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(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); | |
you'd have to do var head = d.head || d.getElementsByTagName("head")[0]; for x-browser anyway.
also this does little more than:
eval("(" + code + ")");
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.
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
Why not: d.head.appendChild(e) ?