views:

75

answers:

3

Image tag (<img src="" alt="" />), line break tags (<br />), or horizontal rule tags (<hr />) have the slashes at the end to indicate itself as self-closing tags. However, when these objects are created by javascript, and I look into the source, they don't have the slashes, making them invalid by W3C standards.

How can I get over this problem?

(I use javascript Prototype library)

+2  A: 

There's is no problem. W3C standards only refer to the markup in the original file, any changes after that are made directly to the DOM, (not your sauce code) which is also a W3C standard. This will in no way affect the standards compliance of your website.

To go into further detail, HTML and XHTML are only different ways of building the DOM (Document Object Model), which is best described as a large tree structure of nodes which is used to describe a web page. Once the DOM has been built, the language used to build it is irrelevant, You can even build the DOM using pure javascript if you wished.

Andrew Dunn
*Sauce* code? Haha :)
alex
@alex: What? Virtual letters are very tasty!
Hello71
+6  A: 

How are you looking at ‘the source’? JavaScript-created elements don't appear in ‘View Source’. Are you talking about innerHTML?

If so then what you are getting is the web browser's serialisation of the DOM nodes in the document. In a browser the HTML markup of a page is not the definitive store for document state. The document is stored as a load of Node objects; when these objects are serialised back to markup, that markup may not look much like the original HTML page markup that was parsed to get the document.

So regardless of which of:

<img src="x" alt="y"/>
<img src="x" alt="y">
<img  alt = "y"    src="x">
img= document.createElement('img'); img.src= 'x'; img.alt= 'y';

you use to create an HTMLImageElement node, when you serialise it using innerHTML the browser will typically generate the same HTML markup.

If the browser is in native-XML mode (ie because the page was served as application/xhtml+html), then the innerHTML value certainly will contain self-closing syntax like<img/>. (You might also see other XML stuff like namespaces too, in some browsers.)

However if, as is more likely today, you're serving ‘HTML-compatible XHTML’ under the media type text/html, the browser isn't actually using XHTML at all, so you'll get old-school-HTML when you access innerHTML and you won't see the self-closing /> form.

bobince
A: 

It never matters!, I used to confirm every standard of W3C but it turns to be a stupid thing! Just conform the safe ones of them that allows you to code cross-browsers and your case is definitely not one of them since it's never an issue and causes no problems.

Omar Dolaimy