javascript - SRC会覆盖写好的JS吗?

标签 javascript

我想我已经明白发生了什么,但我找不到任何相关文档,所以我需要确定:

以 AddThis 为例:

<script type="text/javascript">var addthis_config = {"data_track_addressbar":true};</script>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=example"></script>

我想知道为什么不允许将配置脚本放入 src 调用中:

<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=example"> var addthis_config = {"data_track_addressbar":true}; </script>

我的理论是,当调用 src 时,它实际上会覆盖标签之间编写的代码,但官方的推理和行为是什么?

最佳答案

来自:http://www.jsworkshop.com/articles/02scriptsrc.html

The closing tag is required, and you should not use JavaScript commands between the tags.

来自:http://javascript.crockford.com/script.html

The src attribute is optional. If it is present, then its value is a url which identifies a .js file. The loading and processing of the page pauses while the browser fetches, compiles, and executes the file. The content between the <script src="url"> and the </script> should be blank.

If the src attribute is not present, then the content text between the <script> and the </script> is compiled and executed.

没有解释原因,但我的猜测是有效的 src禁用标签之间任何内容的执行。

另外,请参阅此答案:https://stackoverflow.com/a/6528343/637283

关于javascript - SRC会覆盖写好的JS吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19671349/

相关文章:

javascript - 如何使用 HTML Canvas 通过连接位于等边三 Angular 形三边的三个坐标来查找坐标

javascript - RadioButton 始终返回 false - JavaScript

javascript - 在 JavaScript 中生成 HTML 与加载 HTML 文件

javascript - 缩短 Div 代码

javascript - 如何更改 Three.js 中几何体的基本参数(例如半径、顶点数等)

javascript - JQuery 父 id 选择

javascript - AWS S3 CreateReadStream在循环中仅读取和写入1个文件

读取 url 的 JavaScript 从 html 页面获取特定的 div 标签并将其放置在另一个页面中。

JavaScript 运行时错误 : 'variable' is undefined while checking to see if undefined

javascript - react 路由器 : Data fetched using axios doesn't save when navigating to new route