Javascript

Why self-closing script tags doesn’t work?

Share

Self-closing script tag are not supported by all browsers as this may contain inline code and HTML is not smart enough to turn that feature on or off.

Let’s see two types of script tag.

One is self-closing script tag which browsers do not correctly recognize.

<script src="myscript.js" />

And the another is,

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

which we use generally in every cases.

Does this break the concept of XHTML support anyway?

As XHTML 1 specification says:

Don’t use the minimized form i.e. use <p> </p> and not <p />. Because, the script tag can contain inline code and HTML is not smart enough so that it can turn on or off that feature based on the presence of an attribute.

We can see in our HTML that some of the tags are always self-closed.
For e.g. <hr>Your content here</hr> does not make any sense.
In the same way, there are some tags which cannot be self-closed.
And <script> tag is one of them.

Basically, the concept of self-closing tags is an XML concept. You can use them in XHTML if the document is served with an XML content-type but not if it is served as text/html.

For some of the HTML tags like <img> cannot have any content and thus so they do not have end tags.

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

Read Also: Embedding PDFs without JavaScript

HTML5 allows a “/” character to appear at the end of a start tag for an element which is defined as EMPTY.

So, the <script> element is not allowed to be empty as this may contain inline code, and the HTML is not smart enough to turn that feature on/off based on the presence of the attribute.
So you have to use <script … ></script> every time.

But, on the other hand, HTML have an excellent tag for including any references to outside resource like the <link> tag, and this can be self-closing.

Ans It is already used to include CSS stylesheets, RSS and Atom feeds, canonical URIs etc.

Recent Posts

The Rise of NFTs: Exploring the Impact of Non-Fungible Tokens on the Digital Economy

NFTs, or Non-Fungible Tokens, are revolutionizing the digital economy. These unique digital assets, authenticated through…

3 hours ago

Unveiling the Truth: Is the Spread of Sinus Infections a Myth or Reality?

Sinus infections, impacting approximately 31 million Americans each year, represent a significant health concern stemming…

1 day ago

Best Exercises To Reduce Weight & Keep You Stronger & Fitter!

No doubt that balanced weight is the key to wellness. So, when it comes to…

1 day ago

ARTIFICIAL INTELLIGENCE: Advantages And Disadvantages? Everything You Need to Know

Pros And Cons Of AI: Artificial Intelligence directly translates to conceptualizing and building machines that…

2 days ago

Four common mistakes when picking an internet provider

We all live on the internet; we use it for everything. Thus, when it comes…

2 days ago

What Is Commonly Misdiagnosed as Pink Eye: Understanding Eye Conditions and Their Symptoms

Introduction: Pink eye, or conjunctivitis, is a common eye condition characterized by redness and inflammation…

2 days ago