Context:
我正在构建一个实时 HTML、CSS 和 Javascript 编辑器。
可以访问到here http://experiments.muditameta.com/qckmeddler/.
源码可以访问here https://github.com/zeusdeux/qckMeddler.
问题:
是否可以运行注入到 iframe 中的 javascript 代码,而无需重复删除和添加包含来自 DOM 树的代码的
Thanks!
如果您不介意使用evil eval https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/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(考虑范围),但请注意using eval可能会很糟糕 https://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)