简体   繁体   中英

Importing external js file in html

What is difference between the floowing js importing codes:

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

I realize that the second one is not run. But I wonder differences of them.

It is formal and common to use the first style. So always try to use the formal style of any kind of code. So It is not a problem to be discussed ...

A Self-closing tag is a special form of start tag with a slash immediately before the closing right angle bracket. These indicate that the element is to be closed immediately, and has no content. Where this syntax is permitted and used, the end tag must be omitted. In HTML, the use of this syntax is restricted to void elements and foreign elements. If it is used for other elements, it is treated as a start tag. In XHTML, it is possible for any element to use this syntax. But note that it is only conforming for elements with content models that permit them to be empty.

you can use this Link: http://dev.w3.org/html5/html-author/

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

According to W3C standard http://www.w3.org/XML/ , the above tag is not an acceptable XML tag where you need to immediate close a tag if it does not have anything betweeb opening and closing tag like

<script src="myjs.js" />

However since this tag appears in HTML, HTML is more forgieving

<script> and <style> elements in XHTML sent as text/html have to be
   escaped using ridiculously complicated strings.

   This is because in XHTML, <script> and <style> elements are #PCDATA
   blocks, not #CDATA blocks, and therefore <!-- and --> really _are_
   comments tags, and are not ignored by the XHTML parser. To escape
   script in an XHTML document which may be handled as either HTML4 or
   XHTML, you have to use:

      <script type="text/javascript"><!--//--><![CDATA[//><!--
        ...
      //--><!]]></script>

   To embed CSS in an XHTML document which may be handled as either
   HTML4 or XHTML, you have to use:

      <style type="text/css"><!--/*--><![CDATA[/*><!--*/
        ...
      /*]]>*/--></style>

Taken from http://hixie.ch/advocacy/xhtml

firstly using short tag is a bad practice and The problem with <script /> is not that the specification disallows it, but that browsers don't interpret it as "non-tag-soup"( see here ) if the content type is not application/xhtml+xml . browsers may not appear to understand it that script is closed.

even your eclipse and other developing tools also ask for end tags.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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