Why does document.getElementById work without a parameter?
Author
Message
Staked Member
Posts : 4 Join date : 2011-05-05
Subject: Why does document.getElementById work without a parameter? Fri Jun 17, 2011 9:04 am
I'm a perplexed nube, and I can't understand how document.getElementById evaluates to true when no parameter is passed to getElementById. This code alerts 'foo' ...
Code:
if (document.getElementById) {alert('foo')} else {alert('bar')}
Just to double check, this code alerts 'bar'
Code:
if (!document.getElementById) {alert('foo')} else {alert('bar')}
Pyro Member
Posts : 7 Join date : 2011-04-18
Subject: Re: Why does document.getElementById work without a parameter? Fri Jun 17, 2011 9:06 am
document.getElementById is the address of a function, not a function call.
Twerk Member
Posts : 26 Join date : 2011-04-17
Subject: Re: Why does document.getElementById work without a parameter? Fri Jun 17, 2011 9:07 am
This method is used for feature detection (as opposed to browser detection). Existing functions/features evaluate to true, non-existing ones evaluate to false. In case of true you can safely use the feature independent of the browser/version.
Jake Member
Posts : 2 Join date : 2011-05-09
Subject: Re: Why does document.getElementById work without a parameter? Fri Jun 17, 2011 9:08 am
Anyway, strictly speaking, the condition if(document.getElementById) is perfectly useless, because all the browsers in use now do recognize this method, so that the Boolean return will be always true
Sponsored content
Subject: Re: Why does document.getElementById work without a parameter?
Why does document.getElementById work without a parameter?