Skip to content

Instantly share code, notes, and snippets.

@cowboy
Forked from phiggins42/noteval.js
Created February 20, 2011 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboy/835958 to your computer and use it in GitHub Desktop.
Save cowboy/835958 to your computer and use it in GitHub Desktop.
JavaScript noteval (from phiggins)
// 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