Hallvard B Furuseth said the following on 3/8/2006 12:57 PM:[color=blue]
> Back to FAQ's Q 4.15... I seem to be coming and going - for more
> pleasant reasons than yous, fortunately:-) Off to holiday in a few
> days...[/color]
Add two pulled teeth in the last two days and it gets more fun :) Hope
your holiday was good though.
[color=blue]
> Randy Webb writes:[color=green]
>> Hallvard B Furuseth said the following on 2/26/2006 11:25 AM:[color=darkred]
>>> (blah blah blah)
>>>
>>> tempVar = element.innerHTML;[/color]
>> If the browser doesn't support innerHTML, you will get an error on
>> that line. Attempting to set it first and then read it won't cause an
>> error (if the browser supports setting properties that way).[/color]
>
> Hm? Firefox, IE and Opera do not fail on 'tempVar = element.urgle;'.
> Will some browsers do so? I hope none fail on 'if (document.all)'...[/color]
No it doesn't, but alerting tempVar will give you undefined. Not a very
good test. Not sure why I said that though, I don't remember to be honest.
[color=blue]
> The "Alternative DynWrite()" page does mention a browser which will err
> on attempts to test/read/modify HEAD elements. But it wraps those tests
> in "if (typeof element.innerHTML == 'string')", maybe that avoids the
> problem. Anyway, in my case I'll just avoid .innerHTML for HEAD
> elements. And of input elements. And, and, and...[/color]
I never modify the head via innerHTML so I don't know :) And I don't
create forms with innerHTML either.
[color=blue][color=green][color=darkred]
>>> element.innerHTML = newHTML;
>>> return (tempVar == element.innerHTML); // .innerHTML is writeable
>>> which would return true if innerHTML was changed to chicken.[/color]
>> The test in the FAQ Notes is such that it uses "non-normalized" HTML and
>> reads it back and checks to see if it has been normalized by the browser:
>>
>> <span id = "someID">Some text</span>
>>
>> It has that extra spaces in it. If the browser supports changing the
>> innerHTML then it won't have those extra spaces in the normalized HTML
>> that it returns from reading innerHTML.[/color]
>
> Yup, that part I get, since it is commented in the DynWrite source:-)
> I just didn't use that, my above quotes is about someone else's code.[/color]
Gotcha.
[color=blue][color=green][color=darkred]
>>> (...)
>>> Anyway, I see no reason in the thread you quoted why my function below
>>> might return true if innerHTML is not supported. Note, my "element" is
>>> an element fetched with getElementById or whatever, it's not the
>>> "foobar" element.[/color]
>> I saw that in reading it this time, and not last time. You run into a
>> possible scenario where your function will fail but the browser may
>> support innerHTML.
>>
>> If the browser supports innerHTML but it won't read dynamically inserted
>> ID's, then it will fail, even if it supported changing the innerHTML of
>> an element.[/color]
>
> Yuck. Looks like one should be even more careful about innerHTML than I
> had yet realized.[/color]
innerHTML is expensive in terms of memory also when compared to DOM
methods, some even in IE.
[color=blue]
> Yet the "alternative DynWrite" function also includes such a test, and
> will also fail then. So that's not reason to include a test for whether
> the HTML code was normalized either.[/color]
[color=blue]
> The reason I can think of is still that some browser might do the
> opposite - recognize the dynamically inserted IDs, but not insert the
> HTML. Or it could simply be that with innerHTML it's best to be as
> paranoid as at all possible.[/color]
If it recognized dynamically created/inserted ID's but not innerHTML it
would totally shock me. But, it could happen.
[color=blue][color=green][color=darkred]
>>>>> function testInnerTML(element) {[/color][/color]
> if (typeof element.innerHTML == 'string')[color=green][color=darkred]
>>>>> if (! document.getElementWithId("foobar")) {
>>>>> element.innerHTML = '<span id="foobar">xyzzy</span>';
>>>>> if (document.getElementWithId("foobar"))
>>>>> return true; // Successfully inserted
>>>>> }
>>>>> return false; // Insertion failed
>>>>> }[/color]
>> (...) The test for innerHTML support in the FAQ Notes was very well
>> conceived and written, it took a while to come up with it, along with
>> some very interesting discussions :)[/color]
>
> Having read far too many threads about innerHTML I'm realizing even more
> detail in there than the source discusses - I just don't understand it,
> and would still like to.[/color]
What parts? (You are trying to make me read all those notes aren't you! :)
[color=blue]
> It does have a bug: The inner if() needs "else el.innerHTML = inH;".
> Otherwise, if .innerHTML is partially supported but DynWrite() does not
> realize that, the function will insert <sTrOnG Id='tSt' >test</StRoNg >
> in the document and leave it there. E.g. if the browser updates the
> HTML but not dynamically inserted IDs, like you said above.
> The same applies to my function.[/color]
If I were putting a test in DynWrite, or any other function to test
innerHTML, the element I attempted to insert it in would be hidden via
CSS so that the user wouldn't see it.
[color=blue]
> BTW, any special reason to use <strong>? I'd think something less
> visible would be better for the test, like <span> or <tt>.[/color]
Any reason for Strong versus span? Not that I am aware of. The entire
purpose of that test is to see if the browser normalized it or not, not
necessarily the code it normalized but the normalizing itself.
--
Randy
comp.lang.javascript FAQ -
http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/