< script src =" foo.js">< / script>之间的区别和< script src =" foo.js" />? [英] Difference between <script src="foo.js"></script> and <script src="foo.js"/>?

查看:124
本文介绍了< script src =" foo.js">< / script>之间的区别和< script src =" foo.js" />?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两种将javascript拉入页面的方式有什么区别?

 < script src =foo.js >< /脚本> 

Vs:

 < script src =foo.js/> 

脚本标记不会有子元素,所以不需要分开开始和结束标签,对吧?就像表中的空行一样,您可以执行< tr /> 而不是< tr>< / tr> ; ?或者我在这里基地?

解决方案

总之,第一个是正确的,第二个是不正确的导致您的页面无法正常呈现。



脚本元素不允许自行终止。它必须有一个明确的结束标记。如果省略,浏览器会相信它后面的所有内容仍然是JavaScript,并且不能正确处理它。



请参阅这个简单的示例。这是第二种方法< script src =foo.js/>

 <!doctype html>< html> < HEAD> < script src =doesntmatter.js/> <标题>你好< /标题> < /头> <身体GT; < h1>您是否看到我?< / h1> < / body>< / html>  

正确的版本< script src =foo.js>< / script>
$ b

 <!doctype html>< html> < HEAD> < script src =doesntmatter.js>< / script> <标题>你好< /标题> < /头> <身体GT; < h1>您是否看到我?< / h1> < / body>< / html>  

$ b

< element /> 的终止(XHTML)语法只能用于元素(禁止内容模型 - 即没有为它们定义结束标记,也不是可以具有内容的元素,但你刚刚决定不给它)。 p>

您可能不会将这种语法作为一种便利方法,因为标签会为其定义一个结束标记。
$ b

允许使用自终止语法的标记示例是(from MDN ):




  • < area>

  • < base>

  • < br>
  • < col>

  • < colgroup> 当存在范围时

  • < comman d>

  • < embed> c $ c>< hr>

  • < img> b
  • < input>

  • < keygen>

  • < link>

  • < meta> code>

  • < param> < source>

  • < track> < wbr>




请注意,多年来我一直与其他
Stack Overflow会员进行过多次激烈的辩论(今天的一个事实就是
的事实),其中涉及到使用自行终止的$ b $的最佳做法b元素。


我经常听到的论点是,使用它们会使代码更清晰。
但是,那怎么可能是因为要使用它们,你必须知道
在哪里可以使用它们。而且,如果你知道这一点,那么当你看到< br> 之类的东西时,
代码就很清楚,因为你
已经知道< br> 没有关闭!

然后,这个论点变成了:好吧,对我来说,代码更清晰
,对于其他人来说,可能不知道< br> 不应该被
关闭。这里的反说法是,如果他们不知道
标签是否应该被关闭,他们会在错误的地方使用这个
语法(这个事实不容置疑证明你的问题和许多其他人喜欢它)!

最后一个参数是使用这个语法使得代码有效的XML
,是构建所有HTML的好方法,以防万一您的
HTML需要被解析为XML。那么,这是一个有效的观点。但是,现在大多数用例都是
,我们真的在使用XHTML吗?当然,
可能是这种情况仍然有意义的环境,但肯定不会出现在JSON主流
中。因此,将这种语法推荐为最佳
做法是不合理的。

我强烈建议不要使用自终止元素



What's the difference between these two ways of pulling javascript into a page?

<script src="foo.js"></script>

Vs:

<script src="foo.js"/>

The script tag isn't going to have children, so there's no need to separate the start and end tags, right? Like how if you have an empty row in a table, you can do <tr/> instead of <tr></tr>? Or am I way off base here?

解决方案

In short, the first one is correct and the second one isn't and will cause your page not to render properly.

The script element is not allowed to be self-terminated. It must have an explicit closing tag. If it is omitted, the browser will believe that everything after it is still JavaScript and not process it properly.

See for yourself with this simple example. Here's the second approach of <script src="foo.js"/>:

<!doctype html>
<html>
 <head>
   <script src="doesntmatter.js" />
   <title>Hello</title>
 </head>
 <body>
   <h1>Do you see me?</h1>
 </body>
</html>

And now the correct version <script src="foo.js"></script>:

<!doctype html>
<html>
 <head>
   <script src="doesntmatter.js"></script>
   <title>Hello</title>
 </head>
 <body>
   <h1>Do you see me?</h1>
 </body>
</html>

The self-terminating (XHTML) syntax of <element /> may only be used on empty elements (elements that forbid a content model - i.e. a closing tag is not defined for them, not elements that could have content, but you've just decided not to give it any).

You may not use this syntax as a convenience with tags that do have a closing tag defined for them.

Examples of tags where self-terminating syntax is allowed are (from MDN):

  • <area>
  • <base>
  • <br>
  • <col>
  • <colgroup> when the span is present
  • <command>
  • <embed>
  • <hr>
  • <img>
  • <input>
  • <keygen>
  • <link>
  • <meta>
  • <param>
  • <source>
  • <track>
  • <wbr>

On a personal note, I have had several spirited debates with fellow Stack Overflow members over the years (one just today as a matter of fact) about a best-practice relating to the use of self-terminating elements.

The argument I often hear is that using them makes the code clearer. But, how could that be because in order to use them, you must know where it is acceptable to use them. And, if you know that, then the code is just as clear when you see something like <br>, because you already know that <br> doesn't get closed!

Then the argument becomes, "Well, it's not so that the code is clearer for me, it's for others, who might not know that <br> isn't supposed to be closed." The counter-argument here is that if they don't know which tags are and aren't supposed to be closed, they WILL use this syntax in the wrong places (this fact is not in doubt as evidenced by your question and the many others like it)!

The final argument is that using this syntax makes the code valid XML and that is a good way to structure all your HTML, just in case your HTML needs to ever be parsed as XML. Well, that's a valid point. But, for most use-cases these days, are we really using XHTML? Sure, there may be environments where this still makes sense, but certainly not in the main stream thanks to JSON. So, recommending this syntax as a best- practice wouldn't be warranted.

I strongly advocate against the use of self-terminating elements in non-XML applications, period. Using them buys you nothing and, as you have seen, can lead to bugs in the code.

这篇关于&lt; script src =&quot; foo.js&quot;&gt;&lt; / script&gt;之间的区别和&lt; script src =&quot; foo.js&quot; /&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆