When objects (e.g. img tag) are created using javascript, it does not have a closing tag. How to make it W3C valid?

The image tag ( <img src="" alt="" /> ), line break tags ( <br /> ), or horizontal rule tags ( <hr /> ) have a slash at the end as self-closing tags. However, when these objects are created by javascript, and I look at the source, they have no slashes, which makes them invalid by W3C standards.

How do I solve this problem?

(I am using javascript prototype library)

+2
source share
3 answers

How do you look at the source? JavaScript generated elements do not appear in the 'View Source. Are you talking about innerHTML ?

If so, what you get is the serialization of the web browser of the DOM nodes in the document. In a browser, HTML page layout is not the ultimate repository for document state. The document is stored as loading Node objects; when these objects are serialized back to the markup, that markup may not look like the markup of the original HTML page that was parsed to get the document.

Therefore, regardless of which:

 <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 serialize it using innerHTML , the browser usually generates the same HTML markup.

If the browser is in native-XML mode (i.e. since the page was presented as application/xhtml+html ), then the value of innerHTML will necessarily contain self-closing syntax, for example, <img/> . (In some browsers, you can also see other XML elements, such as namespaces.)

However, if, most likely, today you are using HTML-compatible XHTML under the text/html media type, the browser does not actually use XHTML at all, so you will get the old HTML school when accessing innerHTML and you will not see the self-closing form /> .

+6
source

No problems. W3C standards apply only to markup in the source file, any changes after that are made directly in the DOM (not your sauce code), which is also a W3C standard. This will in no way affect compliance with the standards of your website.

To learn more, HTML and XHTML are just different ways to build the DOM (Document Object Model), which is best described as a large tree structure of nodes that is used to describe a web page. After creating the DOM, the language used to create it does not matter. You can even build a DOM using pure javascript if you want.

+2
source

It never matters !, I used to validate every W3C standard, but it becomes a dumb thing! Just combine those safe ones, which allows you to encode cross-browsers, and your case is definitely not one of them, since it is never a problem and does not cause problems.

0
source

All Articles