最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - <script> tag must include separate <script> tag? - Stack Overflow

programmeradmin5浏览0评论

I noticed today while doing some testing that the way I close my <script> tag either makes or breaks my page. For example, this works:

<script src="scripts/jquery.js" type="text/javascript"></script>

but this does not:

<script src="scripts/jquery.js" type="text/javascript" />

The file appears to show up when I use IE's Developer Tools, but it seems like it just gets ignored. Has anyone ever seen this or know why it might be happening? Thanks in advance!

I noticed today while doing some testing that the way I close my <script> tag either makes or breaks my page. For example, this works:

<script src="scripts/jquery.js" type="text/javascript"></script>

but this does not:

<script src="scripts/jquery.js" type="text/javascript" />

The file appears to show up when I use IE's Developer Tools, but it seems like it just gets ignored. Has anyone ever seen this or know why it might be happening? Thanks in advance!

Share Improve this question edited Dec 13, 2013 at 22:19 lhan asked May 26, 2011 at 21:08 lhanlhan 4,63513 gold badges62 silver badges108 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 14

You must include a closing script tag. The script element is not self closing, even when you're only including an external script.

The <script> tag can only be self-closing in truly valid XHTML documents – that is, a XHTML page served with the Content-Type of application/xhtml+xmland when viewed in a supporting browser (IE8 does not qualify; IE9+ does).

In all other HTML documents, (regardless of what DOCTYPE is declared), the <script> tag is not self-closing and therefore must be closed with a </script>.

Read more in this very detailed answer.

I have also noticed you always need the </script>. It's probably because it requires content between the tags ("" counts), even though you're using src.

发布评论

评论列表(0)

  1. 暂无评论