DO YOU NEED A CONTENT WRITER FOR YOUR BUSINESS?

Your One-Stop Solution for All Content Needs! Click here for more!
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.

Jacob Frazier

Based on United States, Jacob Frazier is a skilled JavaScript developer with over 8 years of experience. He is passionate about change and trying new things, both professionally and personally. He loves startups and is extremely proactive.

Recent Posts

Why is SEO a non-negotiable part of modern marketing?

In a world where digital presence is paramount, the question isn't whether you should do…

1 hour ago

Innovations in Hair Care: Exploring Breakthroughs for Lasting Results

Over the years, people have experimented with various methods to maintain healthy and beautiful hair.…

22 hours ago

How To Strengthen Your Brand-Building Efforts?

Your brand more than developing an attractive and creative logo and infectious motto. It's the…

2 days ago

How To Be Successful And Maximize Compensation In A Personal Injury Case

Introduction Are you someone who has suffered from a personal injury and want to file…

2 days ago

What Are The Biggest Challenges Of Working From Home?

Operating from home has emerged as one of the most popular ways of doing jobs…

3 days ago

Art As An Asset: Will NFT’s Help You Survive Hyperinflation?

If the consequences of our society’s ever-growing debt are what worries you, then it is…

4 days ago