...

View Full Version : object detection



C.O.D.E.N.A.M.E
03-22-2006, 11:25 PM
function doDOMStuff() {
var el = document.getElementById('myElId');
// object detection
if(!document.getElementById) return false;

//or

if(!document.getElementById) return;

// doStuff
}

Kravvitz
03-22-2006, 11:48 PM
You should use the object detection before you try to use the object.

Whether you return nothing or false is up to you. I generally return nothing to keep my code a little shorter.

One reason to return false or something else would be to tell the code that called the function that something went wrong.

C.O.D.E.N.A.M.E
03-23-2006, 02:55 AM
u prefer:

function doDOMStuff() {
if(!document.getElementById) return;
var el = document.getElementById('myElId');

Kravvitz
03-23-2006, 04:10 AM
Correct.

Though I would use something resembling proper indentation. (I like 2 space indetantion; other people seem to prefer 3 or more.)

function doDOMStuff() {
if(!document.getElementById) return;
var el = document.getElementById('myElId');

Kor
03-23-2006, 07:47 AM
which might be the probablity to find a NS4 or an IE4 these days? Does it worth to use if(!document.getElementById) return; on and on, in each function, in each code...hoping that there might be an old Dynosaurus using those old browsers? And giving him nothing but a return ?

C.O.D.E.N.A.M.E
03-23-2006, 12:08 PM
it is not the same level as using document.all or document.layers

It is just a coding esthetic.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum