tag?", "text": "<p>I noticed today while doing some testing that the way I close my <code>&lt;script&gt;</code> tag either makes or breaks my page. For example, this works:</p>\n\n<pre class="prettyprint"><code>&lt;script src="scripts/jquery.js" type="text/javascript"&gt;&lt;/script&gt;\n</code></pre>\n\n<p>but this does not:</p>\n\n<pre class="prettyprint"><code>&lt;script src="scripts/jquery.js" type="text/javascript" /&gt;\n</code></pre>\n\n<p>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!</p>", "answerCount": 3, "upvoteCount": 266, "dateCreated": "2011-05-26 21:08:18", "dateModified": "2022-10-02 07:32:18", "author": { "type": "Person", "name": "lhan" }, "acceptedAnswer": { "@type": "Answer", "text": "<p>You must include a closing script tag. The script element is not self closing, even when you're only including an external script. </p>", "upvoteCount": 144, "url": "https://exchangetuts.com/script-tag-must-include-separate-script-tag-1640627223674552#answer-1653614355611197", "dateCreated": "2022-10-01 18:32:18", "dateModified": "2022-10-02 06:32:18", "author": { "type": "Person", "name": "KatieK" } }, "suggestedAnswer": [ { "@type": "Answer", "text": "<p>The <code>&lt;script&gt;</code> tag can only be self-closing in <em>truly</em> valid XHTML documents – that is, a XHTML page served with the <code>Content-Type</code> of <code>application/xhtml+xml</code> – <strong>and</strong> when viewed in a supporting browser (IE8 does not qualify; IE9+ does).</p>\n\n<p>In all other HTML documents, (<strong>regardless</strong> of what <code>DOCTYPE</code> is declared), the <code>&lt;script&gt;</code> tag is not self-closing and therefore <strong>must</strong> be closed with a <code>&lt;/script&gt;</code>.</p>\n\n<p>Read more in this very detailed answer.</p>", "upvoteCount": 40, "url": "https://exchangetuts.com/script-tag-must-include-separate-script-tag-1640627223674552#answer-1653614360638600", "dateCreated": "2022-09-30 18:32:18", "dateModified": "2022-10-02 07:32:18", "author": { "type": "Person", "name": "josh3736" } }, { "@type": "Answer", "text": "<p>I have also noticed you always need the <code>&lt;/script&gt;</code>. It's probably because it requires content between the tags ("" counts), even though you're using <code>src</code>.</p>", "upvoteCount": 30, "url": "https://exchangetuts.com/script-tag-must-include-separate-script-tag-1640627223674552#answer-1653614360643003", "dateCreated": "2022-10-01 18:32:18", "dateModified": "2022-10-02 06:32:18", "author": { "type": "Person", "name": "Andrea" } } ] } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<script> tag must include separate </script> tag?

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!

like image 266
lhan Avatar asked May 26 '11 21:05

lhan


People also ask

Where is the script </ script tag placed?

The <script> Tag Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

Can we have two script tags in HTML?

An HTML page can contain multiple <script> tags in the <head> or <body> tag. The browser executes all the script tags, starting from the first script tag from the beginning.

How do I include a separate JavaScript file in HTML?

To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.

Where script tag should be placed in HTML?

The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load. Generally, JavaScript code can go inside of the document <head> section in order to keep them contained and out of the main content of your HTML document.


3 Answers

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

like image 144
KatieK Avatar answered Oct 02 '22 06:10

KatieK


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.

like image 40
josh3736 Avatar answered Oct 02 '22 07:10

josh3736


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.

like image 30
Andrea Avatar answered Oct 02 '22 06:10

Andrea