悪 を使用してもかまわない場合はeval
、たとえば、iframeのウィンドウでほとんどのJavaScriptを再評価できます。
function someFunction(){ // any function
console.log(document.body.children);
}
someFunction(); // see normal output
var ifrm = document.getElementsByTagName('iframe')[0], // your iframe
iwind = ifrm.contentWindow; // the iframe's window
iwind.eval( someFunction.toString() ); // re-evaluate function with eval from iframe
iwind.someFunction(); // see new output - output is in iframe's context
と比較する
iwind.someFunction = someFunction; // set variable
iwind.someFunction(); // same as calling someFunction() from parent
最も有効なJavaScript(スコープを考慮)で機能するはずですが、使用eval
が悪い場合があることに注意してください。