-
-
Save cowboy/835958 to your computer and use it in GitHub Desktop.
JavaScript noteval (from phiggins)
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
// Untested mod of phiggins' original gist. Maybe I'll actually try it someday. | |
function noteval( code, doc ) { | |
// add some javascript to a document, like an iframe | |
// | |
// var iframe = document.getElementById('theframe'), | |
// iframeDoc = iframe.contentDocument || iframe.contentWindow.document; | |
// | |
// noteval('alert("hi")', iframeDoc); | |
var parent = doc.head || doc.body || doc.documentElement, | |
scr = doc.createElement('script'), | |
method = 'text' in scr ? 'text' | |
: 'textContent' in scr ? 'textContent' | |
: 'innerHTML' in scr ? 'innerHTML' | |
: null; | |
if ( method ) { | |
scr[method] = code; | |
} else { | |
scr.appendChild( doc.createTextNode(code) ); | |
} | |
parent.appendChild(scr); | |
parent.removeChild(scr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment