By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,599 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,599 IT Pros & Developers. It's quick & easy.

emulating document.documentElement on pre W3C DOM browsers

P: n/a

(Please tell me if this is silly or I am barking up the wrong tree)

Can someone check this please on pre W3C DOM browsers :-

function getDocumentRoot()
{
for ( e in document.childNodes)
if ( document.childNodes[e].nodeName == "HTML")
return document.childNodes[e];
return document.body;
}

if (!document.documentElement)
document.documentElement = getDocumentRoot();

There's an html test page here :-

http://www.aarongray.org/Test/JavaSc...ment-test.html

There's also a test for document.getElementById() emulation here :-

http://www.aarongray.org/Test/JavaSc...ById-test.html

Many thanks in advance,

Aaron
Jul 11 '08 #1
Share this Question
Share on Google+
21 Replies


P: n/a
"Aaron Gray" <an********@gmail.comwrote in message
news:6d************@mid.individual.net...
if (!document.documentElement)
document.documentElement = getDocumentRoot();
Minor behavioural amendment :-

if (!document.documentElement)
{
document.documentElement = getDocumentRoot();
document.documentElement.readOnly = true;
}

Aaron

Jul 11 '08 #2

P: n/a
SAM
Aaron Gray a écrit :
"Aaron Gray" <an********@gmail.comwrote in message
news:6d************@mid.individual.net...
Your page-test works well with my Fx.3
(while it is not W3C compliant)
> if (!document.documentElement)
document.documentElement = getDocumentRoot();

Minor behavioural amendment :-

if (!document.documentElement)
{
document.documentElement = getDocumentRoot();
on my idea the next line will be not used.

'HTML' has been written on the page
so, probably, there is no more existing JS.
document.documentElement.readOnly = true;
}
if (!document.documentElement)
{
document.documentElement = getDocumentRoot();
document.documentElement.readOnly = true;
alert('vu');
}

the alert doesn't fire

--
sm
Jul 11 '08 #3

P: n/a
"SAM" <st*********************@wanadoo.fr.invalidwrote in message
news:48*********************@news.orange.fr...
Aaron Gray a écrit :
>"Aaron Gray" <an********@gmail.comwrote in message
news:6d************@mid.individual.net...

Your page-test works well with my Fx.3
(while it is not W3C compliant)
>> if (!document.documentElement)
document.documentElement = getDocumentRoot();

Minor behavioural amendment :-

if (!document.documentElement)
{
document.documentElement = getDocumentRoot();

on my idea the next line will be not used.

'HTML' has been written on the page
so, probably, there is no more existing JS.
Great.
> document.documentElement.readOnly = true;
}

if (!document.documentElement)
{
document.documentElement = getDocumentRoot();
document.documentElement.readOnly = true;
alert('vu');
}

the alert doesn't fire
Okay Fx.3 does not support the readOnly attribute then.

Thats annoying as AFAICS theres no real fix for that :(

Aaron
Jul 11 '08 #4

P: n/a
On Jul 11, 11:55*am, "Aaron Gray" <ang.use...@gmail.comwrote:
(Please tell me if this is silly or I am barking up the wrong tree)

Can someone check this please on pre W3C DOM browsers :-
Don't do this:
* * {
* * * * for ( e in document.childNodes)
* * * * * * if ( document.childNodes[e].nodeName == "HTML")
* * * * * * * * return document.childNodes[e];
* * * * return document.body;
* * }

for in enumerates and does this up the prototype chain. In Firefox,
"item" will be enumerated, e.g document.childNodes['item']. This is
inefficient and undesirable.

>
Aaron
Jul 11 '08 #5

P: n/a
"dhtml" <dh**********@gmail.comwrote in message
news:78**********************************@l64g2000 hse.googlegroups.com...
On Jul 11, 11:55 am, "Aaron Gray" <ang.use...@gmail.comwrote:
>(Please tell me if this is silly or I am barking up the wrong tree)

Can someone check this please on pre W3C DOM browsers :-
Don't do this:
>{
for ( e in document.childNodes)
if ( document.childNodes[e].nodeName == "HTML")
return document.childNodes[e];
return document.body;
}
for in enumerates and does this up the prototype chain. In Firefox,
"item" will be enumerated, e.g document.childNodes['item']. This is
inefficient and undesirable.
Okay so I use Array iteration rather than object iteration, thus :-

for ( var i = 0, N = document.childNodes.length; i < N; ++i)
if ( document.childNodes[i].nodeName == "HTML")
return document.childNodes[i];
return document.body;

I just learned that lesson else where :)

Thanks,

Aaron
Jul 12 '08 #6

P: n/a
Aaron Gray wrote:
>
(Please tell me if this is silly or I am barking up the
wrong tree)
Yes and Yes.
Can someone check this please on pre W3C DOM browsers :-
There is no need because Netscape 4 and IE 4 have no - childNodes -
collections/nodeLists. IE 4 had - children - collections, but not on the
document object if I recall correctly) and Netscape 4 had no means of
accessing an HTML element (because it could not be made into a layer
with CSS).
function getDocumentRoot()
{
for ( e in document.childNodes)
The for-in statement throws and exception if the expression on the right
of the - in - is not type-convertible into an object, so IE 4 and
Netscape 4 exit here.
if ( document.childNodes[e].nodeName == "HTML")
return document.childNodes[e];
return document.body;
Netscape 4 has no - docuemnt.body - either.
}
<snip>

Richard.

Jul 12 '08 #7

P: n/a
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in message
news:g5*******************@news.demon.co.uk...
Aaron Gray wrote:
>>
(Please tell me if this is silly or I am barking up the
wrong tree)

Yes and Yes.
Right :)
>Can someone check this please on pre W3C DOM browsers :-

There is no need because Netscape 4 and IE 4 have no - childNodes -
collections/nodeLists. IE 4 had - children - collections, but not on the
document object if I recall correctly) and Netscape 4 had no means of
accessing an HTML element (because it could not be made into a layer with
CSS).
> function getDocumentRoot()
{
for ( e in document.childNodes)

The for-in statement throws and exception if the expression on the right
of the - in - is not type-convertible into an object, so IE 4 and Netscape
4 exit here.
Okay that has been moded to an array indexing for loop.
> if ( document.childNodes[e].nodeName == "HTML")
return document.childNodes[e];
return document.body;

Netscape 4 has no - docuemnt.body - either.
Right
> }
<snip>

Richard.
Great, thats cut the lower end off at IE4 and NN4.

Thanks,

Aaron
Jul 12 '08 #8

P: n/a
Aaron Gray meinte:
Okay so I use Array iteration rather than object iteration, thus :-

for ( var i = 0, N = document.childNodes.length; i < N; ++i)
if ( document.childNodes[i].nodeName == "HTML")
return document.childNodes[i];
return document.body;

I just learned that lesson else where :)
Ok. Now add curly brackets - and it looks more like JavaScript ("N"
should become "n", too). You know JSLint [1]?

Gregor

[1]
http://www.jslint.com/
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jul 13 '08 #9

P: n/a
"Gregor Kofler" <us****@gregorkofler.atwrote in message
news:aE*************@nntpserver.swip.net...
Aaron Gray meinte:
>Okay so I use Array iteration rather than object iteration, thus :-

for ( var i = 0, N = document.childNodes.length; i < N; ++i)
if ( document.childNodes[i].nodeName == "HTML")
return document.childNodes[i];
return document.body;

I just learned that lesson else where :)

Ok. Now add curly brackets - and it looks more like JavaScript ("N" should
become "n", too). You know JSLint [1]?

Gregor

[1]
http://www.jslint.com/
Thanks, have not got to Lint'ing things yet. I did not like that N either :)

Aaron
Jul 13 '08 #10

P: n/a
Aaron Gray wrote:
Richard Cornford wrote:
>Aaron Gray wrote:
<snip>
>>Can someone check this please on pre W3C DOM browsers :-
<snip>
>> function getDocumentRoot()
{
for ( e in document.childNodes)
<snip>
>... IE 4 and Netscape 4 exit here.
<snip>
Great, thats cut the lower end off at IE4 and NN4.
So which "pre W3C DOM browsers" come after IE 4 and NN4? I suppose
Konqueror 2 might have fallen into that gap, but its "experimental"
script engine was so far short of any ECMAScript specification that it
could hardly execute scripts at all (and certainly nothing modern (not
having support for, for example, function expressions)).

Richard.

Jul 13 '08 #11

P: n/a
SAM
Gregor Kofler a écrit :
[1]
http://www.jslint.com/
JSLint is completely mad if not crazy !
Only trying to follow how it thinks indentations have to be
gives a code unreadable for me.
--
sm
Jul 13 '08 #12

P: n/a
"Richard Cornford" <Ri*****@litotes.demon.co.ukwrote in message
news:g5*******************@news.demon.co.uk...
Aaron Gray wrote:
>Richard Cornford wrote:
>>Aaron Gray wrote:
<snip>
>>>Can someone check this please on pre W3C DOM browsers :-
<snip>
>>> function getDocumentRoot()
{
for ( e in document.childNodes)
<snip>
>>... IE 4 and Netscape 4 exit here.
<snip>
>Great, thats cut the lower end off at IE4 and NN4.

So which "pre W3C DOM browsers" come after IE 4 and NN4? I suppose
Konqueror 2 might have fallen into that gap, but its "experimental" script
engine was so far short of any ECMAScript specification that it could
hardly execute scripts at all (and certainly nothing modern (not having
support for, for example, function expressions)).
Thanks Richard, that hopefully just about wraps that one up then.

Aaron
Jul 13 '08 #13

P: n/a
"SAM" <st*********************@wanadoo.fr.invalidwrote in message
news:48*********************@news.orange.fr...
Gregor Kofler a écrit :
>[1]
http://www.jslint.com/

JSLint is completely mad if not crazy !
Only trying to follow how it thinks indentations have to be
gives a code unreadable for me.
Oh! Have not had chance to try it out yet ! Could be fun, or maybe not fun
:)

What indenting scheme do you use ?

Aaron
Jul 13 '08 #14

P: n/a
SAM meinte:
Gregor Kofler a écrit :
>[1]
http://www.jslint.com/

JSLint is completely mad if not crazy !
It isn't. It's perfect for tracking down those otherwise extremely hard
to find bugs.
Only trying to follow how it thinks indentations have to be
gives a code unreadable for me.
What indentations? JSLint normally offers explanations why to follow
certain practices and guidelines. My code stays perfectly readable - in
fact I didn't have to change anything of my usual formatting.

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jul 13 '08 #15

P: n/a
SAM
Gregor Kofler a écrit :
SAM meinte:
>Gregor Kofler a écrit :
>>[1]
http://www.jslint.com/

JSLint is completely mad if not crazy !

It isn't. It's perfect for tracking down those otherwise extremely hard
to find bugs.
Perhaps, I don't know.
Which checkboxes do you choose for that ?
>Only trying to follow how it thinks indentations have to be
gives a code unreadable for me.

What indentations? JSLint normally offers explanations why to follow
certain practices and guidelines. My code stays perfectly readable - in
fact I didn't have to change anything of my usual formatting.
He bien, tant mieux.

I use to do ie :

if(truc>0) {
dothat();
if(foo) return;
}

or in expanded, delayed, that I rarely use :

if(truc>0)
{
dothat();
if(foo)
{
return;
}
}
and I think JSLint wants

if ( truc 0 )
{
dothat();
if ( foo )
{
return;
}
}

With these '{' '}' not aligned on the contained instructions
and all this almost blank lines and all these white spaces
I'm lost :-(

I have only a 20" screen.
(my way : 4 lines, JSLint way : 8 lines)

--
sm
Jul 13 '08 #16

P: n/a
SAM meinte:

JSLint doesn't like that.
if(foo) return;
if(foo) { return; } is "correct".

and I think JSLint wants

if ( truc 0 )
{
dothat();
if ( foo )
{
return;
}
}
Nope.

JSLint wants it the Kernighan-Ritchie style

if ( truc 0) {
if(...) {
}
}

It prvents the bugs induced by the auto semicolon insertion. JSLint
doesn't care about indentation.

I have only a 20" screen.
(my way : 4 lines, JSLint way : 8 lines)
No. It becomes less.

Gregor

--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jul 13 '08 #17

P: n/a
SAM
Gregor Kofler a écrit :
>
JSLint wants it the Kernighan-Ritchie style

if ( truc 0) {
if(...) {
}
}
So I didn't check the good checkboxe(s) on my last try of JSLint ?
(recommended options)

--
sm
Jul 13 '08 #18

P: n/a
SAM meinte:
Gregor Kofler a écrit :
>>
JSLint wants it the Kernighan-Ritchie style

if ( truc 0) {
if(...) {
}
}

So I didn't check the good checkboxe(s) on my last try of JSLint ?
(recommended options)
I normally use the default checkboxes.

In your example above JSLint accepts
if()
{..
}

However,
if() foo();
is not accepted.

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jul 13 '08 #19

P: n/a
SAM
Gregor Kofler a écrit :
SAM meinte:
>Gregor Kofler a écrit :
>>>
JSLint wants it the Kernighan-Ritchie style

if ( truc 0) {
if(...) {
}
}

So I didn't check the good checkboxe(s) on my last try of JSLint ?
(recommended options)

I normally use the default checkboxes.

In your example above JSLint accepts
if()
{..
}

However,
if() foo();
is not accepted.
Yes yes yes understood (while these missing {} are optional)

but with my "normal" way ...
30 lines to tell white spaces missing and wrong indentation
Without forgiving unknown variables
>
Gregor

Jul 14 '08 #20

P: n/a
SAM meinte:
Yes yes yes understood (while these missing {} are optional)

but with my "normal" way ...
30 lines to tell white spaces missing and wrong indentation
Without forgiving unknown variables
It says, that it will "hurt your feelings". ;-)

At first I was pretty pissed by the way JSLint was complaining about
everything, too. However, after spending quite some time tracking down a
bug due to an undeclared variable, I frequently check my scripts with
JSLint. You can still ignore the "error messages", once you know what
causes them.

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jul 14 '08 #21

P: n/a
SAM
Gregor Kofler a écrit :
>
At first I was pretty pissed by the way JSLint was complaining about
(...)
You can still ignore the "error messages", once you know what causes them.
And finally, what's the use to continue to use it ?

--
sm
Jul 14 '08 #22

This discussion thread is closed

Replies have been disabled for this discussion.