简体   繁体   English

脚本Src标记中有脚本? <script>(JavaScript等)

[英]Having Script within a Script Src tag? <script> (JavaScript etc)

Why does this not work? 为什么这不起作用?

<script type="text/javascript" src="//cdn.com/assets/js/jquery.js">
alert("Hello World!");
</script>

But this does? 但这样做呢?

<script type="text/javascript" src="//cdn.com/assets/js/jquery.js"></script>
<script type="text/javascript">
alert("Hello World!");
</script>

This is general across many HTML tags that pull from source. 这是从源代码中提取的许多HTML标记的一般性。 Micro optimization is important in my situation and I am also curious. 微观优化在我的情况下很重要,我也很好奇。

From w3.org (emphasis mine): 来自w3.org (强调我的):

If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI. 如果src具有URI值,则用户代理必须忽略该元素的内容并通过URI检索脚本。

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

"If the src attribute is not present, then the content text between the <script> and the </script> is compiled and executed." “如果src属性不存在,则编译并执行<script></script>之间的内容文本。”

As there is a src attribute, the content is not executed 由于存在src属性,因此不会执行内容

In the first example you define the src which makes it IGNORE the contents of the <script></script> 在第一个示例中,您定义了src ,它使IGNORE成为<script></script>

In the 2nd example you have 2 separate <script></script> tags, the 2nd of which is housing your code to execute. 在第二个示例中,您有两个单独的<script></script>标记,其中第二个标记用于存储要执行的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM