为什么开发人员在JavaScript中分离<脚本? [英] Why Do Developers Split Up <script in JavaScript?

查看:55
本文介绍了为什么开发人员在JavaScript中分离<脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多这样的东西:
S =< scr+ipt language = \JavaScript1.2 \> \\\
<! - -\\\
;



为什么他们这样做,是否存在一个应用程序/浏览器,如果您直接使用< script>

解决方案



Javascript外部脚本加载陌生性



取自 bobince的回答


要查看问题,请查看其脚本元素中的顶部
行:

 < script type =text / javascript> 
document.write('< script src =set1.aspx?v = 1234
type =text / javascript>< / script>');
< / script>

所以一个HTML解析器出现,看到
开始< script>标记。在
< script>内,正常< tag>解析
被禁用(以SGML术语,
元素具有CDATA内容)。要在脚本块结束处查找
,HTML
解析器将查找匹配的
close-tag< / script>。



它发现的第一个是字符串文本中的
。 HTML
解析器无法知道它在
字符串文字中,因为HTML解析器
不知道JavaScript
语法的任何内容,它们只知道CDATA。所以
你实际上说的是:

 < script type =text / javascript> 
document.write('< script src =set1.aspx?v = 1234
type =text / javascript>
< / script>

也就是说,一个未结束的字符串文字
和一个未完成的函数调用,这些
导致JavaScript错误而
所需的脚本标记从不写入。



解决问题的常见尝试是


  document.write('...< / scr'+'ipt>'); 


这并不能解释为什么它会在开始标记中完成。


I see so many things like this: S = "<scr" + "ipt language=\"JavaScript1.2\">\n<!--\n";

Why do they do this, is there an application/browser that messes up if you just use straight "<script>"?

解决方案

Have a look at this question:

Javascript external script loading strangeness.

Taken from bobince's answer:

To see the problem, look at that top line in its script element:

<script type="text/javascript">
    document.write('<script src="set1.aspx?v=1234"
                           type="text/javascript"></script>');
</script>

So an HTML parser comes along and sees the opening <script> tag. Inside <script>, normal <tag> parsing is disabled (in SGML terms, the element has CDATA content). To find where the script block ends, the HTML parser looks for the matching close-tag </script>.

The first one it finds is the one inside the string literal. An HTML parser can't know that it's inside a string literal, because HTML parsers don't know anything about JavaScript syntax, they only know about CDATA. So what you are actually saying is:

<script type="text/javascript">
    document.write('<script src="set1.aspx?v=1234"
                           type="text/javascript">
</script>

That is, an unclosed string literal and an unfinished function call. These result in JavaScript errors and the desired script tag is never written.

A common attempt to solve the problem is:

document.write('...</scr' + 'ipt>');

This wouldn't explain why it's done in the start tag though.

这篇关于为什么开发人员在JavaScript中分离&lt;脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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