Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 Why does document.getElementById work without a parameter?

View previous topic View next topic Go down 
AuthorMessage
Staked
Member
Member
Staked

Posts : 4
Join date : 2011-05-05

Why does document.getElementById work without a parameter? Empty
PostSubject: Why does document.getElementById work without a parameter?   Why does document.getElementById work without a parameter? EmptyFri 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')}
Back to top Go down
Pyro
Member
Member
Pyro

Posts : 7
Join date : 2011-04-18

Why does document.getElementById work without a parameter? Empty
PostSubject: Re: Why does document.getElementById work without a parameter?   Why does document.getElementById work without a parameter? EmptyFri Jun 17, 2011 9:06 am

document.getElementById is the address of a function, not a function call.
Back to top Go down
Twerk
Member
Member
Twerk

Posts : 26
Join date : 2011-04-17

Why does document.getElementById work without a parameter? Empty
PostSubject: Re: Why does document.getElementById work without a parameter?   Why does document.getElementById work without a parameter? EmptyFri 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.
Back to top Go down
Jake
Member
Member
Jake

Posts : 2
Join date : 2011-05-09

Why does document.getElementById work without a parameter? Empty
PostSubject: Re: Why does document.getElementById work without a parameter?   Why does document.getElementById work without a parameter? EmptyFri 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
Back to top Go down
Sponsored content




Why does document.getElementById work without a parameter? Empty
PostSubject: Re: Why does document.getElementById work without a parameter?   Why does document.getElementById work without a parameter? Empty

Back to top Go down
 

Why does document.getElementById work without a parameter?

View previous topic View next topic Back to top 
Page 1 of 1

 Similar topics

-
» Can you use a select parameter as a field in the dataset?
» Procedure or function expects parameter which was not
» how ruby/rails work ?
» (AJAX => Div) don't work javascript in the div
» [Question] How does premium membership work?

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: Why does document.getElementById work without a parameter? Edit-trash Useless :: Trash-