views:

208

answers:

2

Hi,

I make innerHTML to get data and store it in database. When i tried in firefox it is coming proper with quotes(" or ') for attributes. But in IE i am not getting the quotes(" or ') for attributes. Is this a browser issue? Any answer for this.

Thanks

+1  A: 

Yes, this is a browser issue, in that that's just how IE does it. Note that quotes aren't mandatory for all attributes in HTML, so that doesn't make IE's output invalid. If you want XHTML you'll have to create it yourself by walking the DOM.

Tim Down
Although, there are indeed cases where IE's `innerHTML` *will* generate invalid HTML. (sigh.)
bobince
+1  A: 

Yes. When you use innerHTML, the browser recreates the HTML based on the DOM elements and attributes in the element. Different browsers do so differently, because until recently there was no standard (innerHTML was a Microsoft innovation which was adopted by nearly everyone else, and is being standardized in HTML5). If your markup looks like this:

<span style="color: red" id="foo">...</span>

IE's innerHTML for that will be:

<SPAN id=foo style="COLOR: red">...</SPAN>

...whereas Firefox and Chrome are pretty close to your original.

What IE gives you is valid HTML (you're allowed to omit the quotes on attributes that don't have spaces in them, and UPPER CASE tagnames are okay too), but not valid XHTML (if that's important for what you're doing).

You can build your own (X)HTML string if you like by traversing the DOM tree yourself, or post-process IE's result.

The good news is that now that innerHTML is being standardized, and the standard clearly says that the result should be valid XML in an XML document (which XHTML documents are), and since Microsoft is more engaged and dedicated to standards in this area than they've been in years, it's likely that IE9 will do better in this regard.

T.J. Crowder
Dammit, I was just typing this! +1
Andy E
Actually IE **does not** always quote attributes where necessary and provide valid HTML. Example: `<span class="/"></span>` will be reported as `<span class=/></span>` ! (Granted this is correct, but unlikely HTML. I just couldn't find a better example for now.)
RoToRa
@RoToRa: That's interesting. It does that with some attributes it knows (`class`, `name`) but not ones it doesn't know (like `data-attr`, for instance). So `<span class="/" data-attr="/">...</span>` comes back as `<SPAN class=/ data-attr="/">...</SPAN>`. How bizarre. I'd have to put on a hard hat and delve into the parsing algorithms to see whether `<SPAN class=/>...</SPAN>` is valid HTML or not. My guess is that it is, actually, even if it looks like a self-closing (invalid) XHTML tag to you and me. :-)
T.J. Crowder
`class="/"` is valid, but `class=/` is not. Quote: "In certain cases, authors may specify the value of an attribute without any quotation marks. The attribute value may only contain letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45), periods (ASCII decimal 46), underscores (ASCII decimal 95), and colons (ASCII decimal 58)." http://www.w3.org/TR/REC-html40/intro/sgmltut.html#h-3.2.2
RoToRa
@RoToRa: Thanks. See? I said I'd have to delve into it to be sure. :-) The HTML5 syntax (http://www.w3.org/TR/html5/syntax.html#attributes) is more permissive and would actually allow `class=/`, although I *think* only if it's followed by a space, not the `>` for the tag.
T.J. Crowder