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 :)