Possible Duplicate:
Why split the <script> tag when writing it with document.write()?
查看 enyo 源代码,我看到了这一点(在 enyo.js 中):
document.write('<scri' + 'pt src="' + root + "/source/boot/" + inSrc + '"></scri' + 'pt>');
为什么是<script
标签分为 <scri + pt
?对于结束标记也是如此。这是我不知道的Javascript忍者的 secret 吗?
最佳答案
当浏览器的 html 解析器看到字符串“</script>
”时,无论它是否在 javascript 字符串中,它都会将其视为结束脚本标记并结束当前脚本 block 。将“</script>
”标记分成两部分可以防止在您需要将其作为 JavaScript 字符串时发生这种情况。
参见this explanation :
the tag is content-agnostic. Which means the HTML Parser doesn’t know we’re in the middle of a JavaScript string.
The processing of JavaScript doesn’t happen until after the browser has understood which parts are JavaScript. Until it sees that close tag, it doesn’t care what’s inside – quoted or not.
关于javascript - 为什么脚本标签被分成两个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12302082/