tags:

views:

32

answers:

2

Hello,

I am detecting browsers and trying to apply style,

alert(BrowserDetect.browser);
if(BrowserDetect.browser == "Opera") {
    document.getElementById(myBox).style.paddingTop = "5px";
}

Alert Shows the browsers name accurately but why is the style not getting applied?

+2  A: 

Try adding an alert inside the if and see if it executes. Chances are, there's whitespace around the Operastring.

Krof Drakula
I alerted inside from if statement, it still alerts me but style doesn't gets applied, I have tried applying other styles too.
Shishant
I doubt it. He appears to be using the QuirksMode browser detection JS (http://quirksmode.org/js/detect.html), which is pretty robust
Warren Young
I am using same script.
Shishant
+1  A: 

Put two copies of the alert() call inside the if statement, one before the style assignment and one after. I think you'll find that the second doesn't happen, for any of several reasons:

  1. The variable myBox doesn't exist
  2. myBox has a value that is not an ID of a valid DOM element
  3. myBox does name a valid element, but it is not a block type element, so padding doesn't apply

You can also try running the same code in another browser with a solid JS debugger, like Firefox + Firebug or Safari with the Develop tools enabled. (Preferences | Advanced.) This may lead you to the problem with that style assignment faster.

Warren Young
I am unable to apply styles to any other element, The id myBox exists.
Shishant
That last sentence...do you mean there is an HTML element with id="myBox"? If so, your problem is a Javascript syntax error. You need to say getElementById('myBox') instead. As you've written it, myBox has to be a string variable containing the ID of the element.
Warren Young