首页 > 解决方案 > 进行评估的可能方法

问题描述

我知道的是eval,FunctionsetTimeout。尽管setImmediatereference 没有提到可以使用字符串参数调用它,但我认为它的工作方式与setTimeout这方面相同。

从浏览器中的字符串评估代码的可能方法是什么(包括非标准方法)?

标签: javascript

解决方案


在浏览器上,我唯一知道的是:

  • eval
  • 构造Function函数
  • setTimeout及相关(setInterval,非标准setImmediate
  • 创建脚本元素,设置其文本内容,并将其附加到文档(通过 DOM 方法,或使用document.write或类似方法)
  • 在链接等上使用javascript:伪协议(然后人为地点击它们或邀请用户这样做)
    • 书签是这个的一个特例
  • DOM0 事件处理程序(然后人为触发它们或邀请用户这样做)(很好的一个GOTO 0

居住:

eval("console.log('eval');");
(0,eval)("console.log('indirect eval');");

new Function("console.log('Function constructor');")();

setTimeout("console.log('setTimeout and such');", 0);

var script = document.createElement("script");
script.textContent = "console.log('script element');";
document.body.appendChild(script);

var link = document.createElement("a");
link.href = "javascript:console.log('javascript: pseudo-protocol');";
document.body.appendChild(link);
link.click();

var div = document.createElement("div");
div.setAttribute("onclick", "console.log('DOM0 event handler');");
document.body.appendChild(div);
div.click();
/* Or to be long-winded
div.dispatchEvent(new MouseEvent("click", {
  view: window,
  bubbles: true,
  cancelable: true
}));
*/


推荐阅读