javascript - 为什么脚本标签被分成两个字符串?

标签 javascript optimization enyo

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/

相关文章:

javascript - 如何使用带有 gulp-replace 的捕获组?

javascript - Angular 2 : how to control the whole app with keyboard only?

javascript - 渲染动态 Enyo 中继器

javascript - Order By (alias) then Order by second sequelize

javascript - alert() 和 console.log() 在 Firefox 26 中不起作用

performance - 编译器优化的函数式代码比命令式代码执行得更好的示例

c - 在 C 中展开 For 循环

javascript - 如何在 enyo 中使用 SOAP?

javascript - 排序时,表体为空

python - 如何优化我的 PageRank 计算?