我想我已经明白发生了什么,但我找不到任何相关文档,所以我需要确定:
以 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
禁用标签之间任何内容的执行。
关于javascript - SRC会覆盖写好的JS吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19671349/