Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close HTML Script Tag [duplicate]

I just curious why my JavaScript can't run (tested in Firefox and IE) if I write the <script> tag like this:

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

It will work if I change that line to:

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

So my question is: why I can't close the script tag in the start tag since I don't have any content inside it.

My JavaScript code is simple, just:

alert("test");

Notes: I check firefox error console and no error.

like image 731
Iswanto San Avatar asked Apr 09 '13 08:04

Iswanto San


People also ask

How do you end a HTML script?

HTML tags are either self closing or not. It has nothing to do with the / at the end. That's an XML thing, not an HTML thing. So for example <script> is never self closing because it might contain a code block, and tags like <br> <img> and <meta> are always self closing.

Does script have closing tag?

Dynamic script such as text/javascript . None, both the starting and ending tag are mandatory.

Does script go after body?

In HTML, JavaScript code is inserted between <script> and </script> tags. You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

Can I have 2 scripts 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.


1 Answers

The concept of self-closing tags is an XML concept. You can't use them in HTML. (You can use them in XHTML, but only if the document is served with an XML content-type, not if it is served as text/html).

In HTML some elements (such as <img>) cannot have any content, so they don't have end tags).

Since a script can have a src attribute or the script be can inside the element, <script> is not one of them.

(HTML 5 allows a / character to appear at the end of a start tag for an element that is defined as EMPTY, but it is just sugar for people addicted to XML and has no meaning in the language).

like image 111
Quentin Avatar answered Oct 29 '22 00:10

Quentin