【问题标题】:JSLint "document.write can be a form of eval" - How is this so?JSLint “document.write 可以是 eval 的一种形式” - 这是怎么回事?
【发布时间】:2011-03-30 15:59:28
【问题描述】:

我在 JSLint 中遇到过这条消息...

document.write 可以是 eval 的一种形式。

想知道究竟是怎么回事?

JSLint instructions 页面声明:

eval 函数...提供对 JavaScript 的访问 编译器。这有时是必要的, 但在大多数情况下,它表明 存在极其糟糕的编码......

那么,document.write 如何“提供对 JavaScript 编译器的访问”呢?

谢谢

【问题讨论】:

标签: javascript jslint


【解决方案1】:

您的浏览器对此做了什么?

document.write('<script type="text/javascript">window.alert("evaled " + (1 + 2))</script>');

【讨论】:

  • @Rocket:好像我有点过时了,type 属性现在是首选。但不管是否被弃用,它都有效。
  • @Ben:是的,它有效,但这可能是因为浏览器首先忽略了language 属性。
  • @Rocket:这是正确的现代等价物吗?我只涉足 DHTML。
  • @Ben:是的,没错。 :-) 在 HTML5 中,甚至不需要类型 JavaScript。
  • 回到主题——我想我明白了。 document.write 本身正在写出 JavaScript,然后访问编译器。因此,将其压缩为 document.write('&lt;script&gt;alert(1 + 2)&lt;/script&gt;'); 大致相当于 alert(eval(1+2));
猜你喜欢
  • 2011-08-25
  • 2011-07-06
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 2022-01-23
相关资源
最近更新 更多