为什么我收到这个jsfiddle错误,document.write可以是eval的一种形式 [英] Why am i receiving this jsfiddle error, document.write can be a form of eval

查看:117
本文介绍了为什么我收到这个jsfiddle错误,document.write可以是eval的一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试我在阅读书籍时找到的代码。我在JS小提琴中测试时出现此错误, document.write 可以是eval的一种形式。

I am testing a code I found while reading a book. I get this error while testing it out in JS fiddle, document.write can be a form of eval.

     var text = '<html><body bgcolor=linen><p>' +
    'This is <b>bold<\/b>!<\/p><\/body><\/html>';

var tags = /[^<>]+|<(\/?)([A-Za-z]+)([^<>]*)>/g;
var a, i;
while ((a = tags.exec(text))) {
    for (i = 0; i < a.length; i += 1) {
        document.writeln(('// [' + i + '] ' + a[i]).entityify());
    }
    document.writeln();
}   

我在两行上都收到了上面的JSfiddle警告 document.writeln()

I am getting the above JSfiddle warning on both lines with document.writeln().

推荐答案

请注意,这只是一个警告 - 但是很好一个应该受到尊重的人。它实际上是由一个名为 JSLint 的检查程序生成的 - 并且有一个很好的阅读原因警告可在 http://www.jameswiseman.com/blog/2011/03/31/jslint-messages-document-write-can-be-a-form-of-eval/

Note that this is a warning only - but a good one that should be respected. It is actually being generated by a checker called JSLint - and a good read for the reasoning of this warning is available at http://www.jameswiseman.com/blog/2011/03/31/jslint-messages-document-write-can-be-a-form-of-eval/.

基本上,这是 eval is evil - document.write 可用于执行评估。

Basically, the foundation of this is that "eval is evil" - and that document.write can be used to perform evaluations.

除此之外 - 在上面没有提到,尽可能避免 document.write ,除非是简单的测试。它在被认为是完整之后写入DOM,此时只能使用支持的DOM方法进行修改。有关此问题的其他详细信息,请参阅为什么document.write被视为糟糕的做法"? - 提到它使用安全和DOM友好更好 DOM操作方法 document.createElement element.appendChild 等)。有一个很好的具体例子可以在 https:// developer .mozilla.org / zh-CN / docs / Web / API / Document_object_model / Using_the_W3C_DOM_Level_1_Core

Besides this - and not mentioned in the above, avoid document.write whenever possible, except for maybe simple testing. It writes to the DOM after it is considered to be "complete", and modifications at this point should only be made using the supported DOM methods. Additional details concerning this are covered at Why is document.write considered a "bad practice"? - where it is mentioned that it is "Far better to use the safe and DOM friendly DOM manipulation methods" (document.createElement, element.appendChild, etc.). A good concrete example of this is available at https://developer.mozilla.org/en-US/docs/Web/API/Document_object_model/Using_the_W3C_DOM_Level_1_Core.

这篇关于为什么我收到这个jsfiddle错误,document.write可以是eval的一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆