Skip to Navigation or
Skip to Content

JavaScript Short Tags and XHTML

Feeds

RSS Feed

<< April | May | June >>

Thursday, 20th May 2010

Here is another JavaScript question I was asked yesterday, which was catching out a developer using XHTML and an external JavaScript file. Users of XHTML will be highly familiar with the use of the "short tag" (this isn't when you sell a load of tags you don't yet have, speculating that the value of the tags will go down!) A short tag is an empty element that you close using a slash-finale, such as this:

<img src="myimage.png" alt="This image ends with a short tag" />

On the whole, this technique was introduced into XHTML to solve the problem of the elements such as img or br in the body or the meta tags in the head of the document, which have no closing tag in the HTML specification. By using a short tag, you have valid XML.

Be careful though, as attempting to use a short tag script tag will lead to trouble. So you can't do this:

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

The use of the short tag closing the script tag results in trouble in many browsers, so make sure you add a full closing tag instead, link this:


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

 

You Are Here: Home » Blog » JavaScript Short Tags and XHTML


I use a cookie on this website. This cookie doesn't contain or relate to any personal information and it isn't shared with any other website, it just ensures that I don't count you more than once in my website statistics. The Privacy and Electronic Communications Regulations require me to ask your permission to use this cookie, so please indicate below that you are happy for me to do this - I will remember your selection with a cookie, so if you accept I won't ask again...