ceth
ceth

Reputation: 45295

Check whether the browser understand javascript

I am studying javascript and in my book I have such code:

if(document.getElementsByTagName) {
  if(document.getElementById) {
    if(document.getElementById("some_id") 
      ...

It is common practice to check the browser ability to use javascript or all modern browsers can work with javascript propertly and I can omit the first two conditions?

Upvotes: 0

Views: 315

Answers (2)

darioo
darioo

Reputation: 47183

That code won't even get executed in a browser that doesn't support Javascript, or has it turned off. Because of that, use only

if(document.getElementById("some_id")) {
    // meaningful code here
}

Upvotes: 0

Betard Fooser
Betard Fooser

Reputation: 526

Certainly all modern browsers support javascript, at first thought I was wondering if maybe they were trying to account for users that may have javascript disabled, but this is obviously not the case either. I would say the first 2 conditions are redundant.

Upvotes: 1

Related Questions