August 20, 2010

JavaScript gets rid of trailing slash in <img /> tag

Question by ashurexm

I have the following JS code:

validateConfigName.html('<img src="/rta/images/check-24.png" />');

But when it executes in Firefox I see this as the generated code:

<img src="/rta/images/check-24.png">


Answer by Nick Craver

In HTML the <img> tag is supposed to be <img>, in XHTML it’ll be <img />…so depending on what DOCTYPE your page is using, this will vary.

From the HTML 4.0 Spec for <img>:

Start tag: required, End tag: forbidden

In XHTML elements must be closed:

Well-formedness is a new concept introduced by [XML]. Essentially this means that all elements must either have closing tags or be written in a special form (as described below), and that all the elements must nest properly.

Answer by Starx

It’s the way, the browser renders your html code according to the doctype used.

Nothing to worry about though..

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!