-Lost said:
RobG wrote:
simple question:
I have a simple <dt>test text</dt>
I get the innerHTML of that dt, and I try and append some text to it
like so:
dt = document.getElementsByTagName('dt')[0]
var text = dt.innerHTML + 'new'
in Firefox, I get "test textnew"
and in IE I get "test text new" (with a space)
The unsatisfying answer is that innerHTML is a proprietary Microsoft
invention that has been widely copied by other browsers and there is
no public standard for its implementation. A small amount of testing
in different browsers will show there are a number of differences
across browsers.
Opera, which usually follows IE pretty closely, does the same as
Firefox in this case.
innerHTML is usually OK for inserting HTML, however it starts to fall
apart if you depend upon it for anything else, particularly if you
expect it to be the same as the HTML source.
What alternatives are there if you desire the source to be the same?
I don't know of any reliable, cross-browser way to ensure that an
element's innerHTML is the same as its source in anything other than
trivial cases. There is certainly no way to ensure that any two
browsers will report the same value for the innerHTML property of a
non-trival element.
Modifying a table or a complex structure, modifying attribute values,
there are likely many more.
OK, I am not liking how I worded all that. Sorry.
What alternatives, aside from createElement, createTextNode,
setAttribute, and appendChild are there?
None?
There are a variety of standard and non-standard ways to modify the
DOM, e.g. new Option, that are widely supported and "better" than
innerHTML in certain circumstances. My opinion is that innerHTML
should be reserved for simple cases, such as where text content is to
be replaced.
And in the DOM fashion your source will be exactly as you created it?
Browsers read the source and parse it to create a DOM. According to
MSDN, the innerHTML property:
"Sets or retrieves the HTML between the start and end tags of the
object."
<URL:
http://msdn2.microsoft.com/en-us/library/ms533897.aspx >
The phrase "the HTML" seems to indicate that getting an element's
innerHTML property returns the browser's reverse engineering of a DOM
fragment back to HTML. It is not the literal source that created it,
otherwise they would have said "the source HTML" or such (though given
the generally dreadful quality of MSDN documentation I'm not too sure
about that).
Unless you set out to write the source to be the same as the innerHTML
property, you can almost guarantee that the two will be different to
varying degrees and that any two browsers will also vary in their
innerHTML property.
The reason I ask actually is because of the code below.
<p id="p1" onclick="json1['widget']['window'].winFunc1(this.onclick);">
alert json bracket</p>
<p id="p2" onclick="json1.widget.window.winFunc1(this.onclick);">
alert json dot</p>
The winFunc1 function simply alerts what is passed. In both cases you get:
function onclick(event) {
json1.widget.window.winFunc1(this.onclick);
}
It isn't "what is passed", otherwise it would be the literal source.