[英]Why split the <script> tag when writing it with document.write()?
Why do some sites (or advertisers that give clients javascript code) employ a technique of splitting the <script>
and/or </script>
tags up within document.write()
calls?为什么某些网站(或为客户提供 JavaScript 代码的广告商)采用一种技术,将<script>
和/或</script>
标记拆分到document.write()
调用中?
I noticed that Amazon does this as well, for example:我注意到亚马逊也这样做,例如:
<script type='text/javascript'>
if (typeof window['jQuery'] == 'undefined') document.write('<scr'+'ipt type="text/javascript" src="http://z-ecx.images-amazon.com/images/G/01/javascripts/lib/jquery/jquery-1.2.6.pack._V265113567_.js"></sc'+'ript>');
</script>
<\/script><\/code> has to be broken up because otherwise it would end the enclosing
<script><\/script><\/code> block too early.
<\/script><\/code>必须被分解,否则它会过早结束封闭的
<script><\/script><\/code>块。
Really it should be split between the
<<\/code> and the
\/<\/code> , because a script block is supposed (according to SGML) to be terminated by any end-tag open (ETAGO) sequence (ie
<\/<\/code> )<\/a> :
实际上它应该在
<<\/code>和
\/<\/code>之间分开,因为脚本块应该(根据 SGML)
被任何结束标签打开(ETGO)序列(即
<\/<\/code> )终止<\/a>:
<\/blockquote>
<script type="text\/javascript"> document.write('\\x3Cscript type="text\/javascript" src="foo.js">\\x3C\/script>'); <\/script><\/code><\/pre>"
Here's another variation I've used when wanting to generate a script tag inline (so it executes immediately) without needing any form of escapes:这是我在想要生成内联脚本标记(因此它立即执行)而不需要任何形式的转义时使用的另一种变体:
<script>
var script = document.createElement('script');
script.src = '/path/to/script.js';
document.write(script.outerHTML);
</script>
I think is for prevent the browser's HTML parser from interpreting the <script>, and mainly the <\/script> as the closing tag of the actual script, however I don't think that using document.write is a excellent idea for evaluating script blocks, why don't use the DOM...我认为是为了防止浏览器的 HTML 解析器解释 <script>,主要是 <\/script> 作为实际脚本的结束标记,但是我不认为使用 document.write 是评估脚本的好主意块,为什么不使用 DOM...
var newScript = document.createElement("script");
...
The <\/script><\/code> inside the Javascript string litteral is interpreted by the HTML parser as a closing tag, causing unexpected behaviour ( see example on JSFiddle<\/a> ).
Javascript 字符串中的
<\/script><\/code>被 HTML 解析器解释为结束标记,导致意外行为(
参见 JSFiddle 上的示例<\/a>)。
To avoid this, you can place your javascript between comments (this style of coding was common practice, back when Javascript was poorly supported among browsers).为了避免这种情况,您可以将您的 javascript 放在注释之间(这种编码风格是常见的做法,当 Javascript 在浏览器中的支持很差时)。 This would work (
see example in JSFiddle<\/a> ):这将起作用(参见 JSFiddle 中的示例<\/a>):
<script type="text/javascript">
<!--
if (jQuery === undefined) {
document.write('<script type="text/javascript" src="http://z-ecx.images-amazon.com/images/G/01/javascripts/lib/jquery/jquery-1.2.6.pack._V265113567_.js"></script>');
}
// -->
</script>
The solution Bobince posted works perfectly for me. Bobince 发布的解决方案非常适合我。 I wanted to offer an alternative method as well for future visitors:我还想为未来的访客提供另一种方法:
if (typeof(jQuery) == 'undefined') {
(function() {
var sct = document.createElement('script');
sct.src = ('https:' == document.location.protocol ? 'https' : 'http') +
'://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js';
sct.type = 'text/javascript';
sct.async = 'true';
var domel = document.getElementsByTagName('script')[0];
domel.parentNode.insertBefore(sct, domel);
})();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.