Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-30-2011, 03:55 PM   PM User | #1
X-RaY
New to the CF scene

 
Join Date: Jun 2011
Location: Netherlands
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
X-RaY is an unknown quantity at this point
Macintosh Strange behavior getElementById() in Safari/Firefox

So I've started learning Javascript some days ago.
Reading 'JavaScript Bible' Seventh Edition.

One of the things the book repeatedly says is:
use:
Code:
if(!document.getElementById()) return;
(and others like:
Code:
if(!document.createTextNode()) return;
)
to filter out all the browser who are in fact not supporting DOM.

As I'm using Safari (and Firefox for testing purpose) the 'document.getElementById()' and therefore: DOM is supported.

Now, my browsers (both Safari and Firefox) get returned out of the javascript anyway.

as simple as:
Code:
if(!document.getElementById()) return;
document.write("testIt!");
the document.write doesn't get executed.

Now: I found a simple solutions which seems to work.... partially:
Code:
if(document.getElementById() == "undefined") return;
document.write("testIt!");
This seems to work for Safari, but Firefox still gets returned out of the javascript.

I could just omit the if statements to make it work altogether.
But that doesn't sound like the best method to me, because (so I've read)
it can crash browsers and such.

Now my question: Why are my browsers (or javascript for all I care) acting so strange?
Is there a way to make the if(!document.getElementById()) work?
or is it just a silly rule the writers of the JavaScript Bible made up?

Thanks in advance for any help :)

Last edited by X-RaY; 06-30-2011 at 06:04 PM.. Reason: Resolved
X-RaY is offline   Reply With Quote
Old 06-30-2011, 04:26 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 530 Times in 524 Posts
devnull69 will become famous soon enough
Are you really sure the book says this (literally letter by letter)?
Code:
if(!document.getElementById()) return;
Because this is rubbish. Which book is it?

Correct
Code:
if(!document.getElementById) return;
The first code wants to check whether the result of the call of document.getElementById() without any parameters is null, empty, false or similar. But getElementById() requires a parameter, otherwise it is a logical error which results in an exception and which explains what is happening to you

The second code checks if the method object getElementById exists for the document. If not it will just return without doing anything.
devnull69 is offline   Reply With Quote
Old 06-30-2011, 06:04 PM   PM User | #3
X-RaY
New to the CF scene

 
Join Date: Jun 2011
Location: Netherlands
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
X-RaY is an unknown quantity at this point
Eeks... that's it.
Old habits die hard :()

Book was correct, I typed to fast...

Thanks a bunch!
----
X-RaY is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:51 AM.


Advertisement
Log in to turn off these ads.