简体   繁体   English

这是什么意思:“document.write('<scr'+'ipt ...”?

[英]What does this mean: “document.write('<scr'+'ipt… ”?

I've seen this on every Yahoo! 我在每个Yahoo!上看过这个 news page , at the bottom of the source code, 新闻页面 ,在源代码的底部,
and failed to understand why they break the script word like that. 并且无法理解为什么他们会破坏这样的剧本词。

Does anybody know if there's any reason for this? 有人知道这有什么理由吗?

document.write("<scr"+"ipt language=javascript src=http://l.yimg.com/d/lib/bc/bc_2.0.4.js></scr"+"ipt>");

Consider this simplified example: 考虑这个简化的例子:

<script>
document.write("something </script> something");
</script>

The browser's HTML parser would see the </script> within the JavaScript string and interpret that as the end of the script element. 浏览器的HTML解析器将在JavaScript字符串中看到</script>并将其解释为脚本元素的结尾。

The HTML parser doesn't know about JavaScript syntax - all it knows is that the <script> element ends at the next </script> . HTML解析器不知道JavaScript语法 - 它只知道<script>元素在下一个</script>

(It also knows that you can't have nested <script> elements, hence the breaking of the opening <script> as well as the closing </script> in your example.) (它也知道你不能拥有嵌套的<script>元素,因此在你的例子中打开了<script>以及结束</script> 。)

Suppose you are writing a tool that detects the beginning and end of script blocks in a chunk of text. 假设您正在编写一个工具,用于检测一大块文本中脚本块的开头和结尾。 Suppose you see 假设你看到了

<blah><blahdeblah><script>

blah blah blah

blah

print("</script>")

print("<script>")

blah

</script>

</blahdeblah></blah>

Without knowing the syntax of the script language, how does your tool know that this is ONE script block and not TWO script blocks with ")blah between them? 在不知道脚本语言的语法的情况下,您的工具如何知道这是一个脚本块而不是两个脚本块,并且它们之间有“等等”?

A web browser is such a tool. Web浏览器就是这样一种工具。 It's a reasonable practice to make sure you never confuse the web browser by never having <script> or </script> in your file unless it actually is a script tag. 这是一种合理的做法,确保您永远不会混淆Web浏览器,因为文件中永远不会有<script></script> ,除非它实际上是一个脚本标记。

这样它就不会被评估,而是作为字符串插入。

这是阻止XML / XHTML和HTML验证器对源代码大吼大叫的一种坏方法。

Some browsers tend to "act" to fast when parsing a document and immediately try to execute the javascript when they find a script tag (even though it is itself in a piece of js). 有些浏览器在解析文档时倾向于“快速行动”,并在找到脚本标记时立即尝试执行javascript(即使它本身在一块js中)。 To avoid this they break the decalration of the tag. 为了避免这种情况,他们打破了标签的decalration。

For a full discussion of this, see: 有关此问题的完整讨论,请参阅:
http://www.wwco.com/~wls/blog/2007/04/25/using-script-in-a-javascript-literal/ http://www.wwco.com/~wls/blog/2007/04/25/using-script-in-a-javascript-literal/

The short answer is that your code is parsed in two discrete steps. 简短的回答是您的代码分两个不同的步骤进行解析。

The first one is XML. 第一个是XML。 And that means that the element <SCRIPT> is looking for a </SCRIPT>. 这意味着元素<SCRIPT>正在寻找</ SCRIPT>。 It's important to remember that XML elements are content agnostic. 重要的是要记住XML元素是内容不可知的。 That means that the parser doesn't know yet that there's JavaScript in there. 这意味着,分析器不知道存在的JavaScript在那里。

Once it has the contents of the <SCRIPT> element, then it processes that chunk of text, which presumably is JavaScript. 一旦它具有<SCRIPT>元素的内容, 然后将其处理文本的该块,这大概是JavaScript的。

By splitting up the tag with a string concatenate operator you prevent a constant from tripping up the XML phase. 通过使用字符串连接运算符拆分标记,可以防止常量跳过XML阶段。

One simple solution is to put &lt; 一个简单的解决方案是将&lt; and &gt; 和&gt; in the Javascript text. 在Javascript文本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM