tags:

views:

65

answers:

4

Hi!

I know that there is a difference between ff and ie regarding the attribute 'class'.

I want to put in the global.js something like that:

function GetVarByBrowser()
{
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var getClass= 'className';
  }
  else
  {
    var getClass= 'class';
  }

}

and in the page i added this:

ElementServer.setAttribute(GetVarByBrowser(), 'server');

how can i make it works?

+2  A: 

No need for global variables. Have the function return the value instead:

function GetVarByBrowser()
{
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    return 'className';
  }
  else
  {
    return 'class';
  }

}

However, this is an imperfect method of browser detection for a number of reasons. Have you considered using a library like jQuery that takes care of quirks like this automatically? Or do you want to learn this yourself (which is perfectly fine and commendable)?

Pekka
thank you! i will check jQuery.
Ronny
-1 for browser detection. This won't work in IE8 in Standards Mode.
RoToRa
@RoToRa what he does in the function was not really the issue here, returning the value was. I am explicitly recommending using jQuery.
Pekka
@Pekka True, but showing such broken methods to a beginner is very bad IMHO.
RoToRa
@RoToRa I agree, but the issue at hand was how to change the OP's function so it returns a value. The example wouldn't have made sense with more changes. jQuery is the way to go here anyway.
Pekka
+2  A: 

It's best to just use the x.className getter/setter property, which avoids needing to call attribute functions, and works on all browsers just the same.

Delan Azabani
@Delan: *"When using getAttribute and setAttribute, you always use class because that is the attribute name..."* Oh if only that were true. Internet Explorer (at least through IE7, haven't tested IE8 or IE9) requires you to use "className" even with `setAttribute`. Yes, it's wrong, but that's the way it is. Also "htmlFor" when doing the `for` attribute. (Quite right that the reflected property is always `className`, though.)
T.J. Crowder
Wait... really? Oh... wow. I learn something new about IE's crap every day... ;D
Delan Azabani
@Delan: Yup, you've probably been using a library that insulates you from this rubbish. :-) Try this in IE6 (still (!!) a major browser), for instance: http://jsbin.com/evobu Just tested IE8, and pleased to say that at least *that's* fixed now.
T.J. Crowder
I don't want to come off as arrogant or something else negative, but I've actually been avoiding libraries *because* they slow down the flow of users away from IE. I try not to support IE (except for clients which want IE support).
Delan Azabani
@Delan: LOL! Fair 'nuff, though you're wearing a bit of a hairshirt there -- you can not support IE without denying yourself the *other* benefits libraries like jQuery, Prototype, Closure, and the like can bring you! BTW, you might want to edit or remove this answer, since it's incorrect in its current form.
T.J. Crowder
Done. Thanks for letting me know!
Delan Azabani
A: 

I'd suggest a slightly different approach:

var setClassAttribute(e, value);
if (navigator.appName == 'Microsoft Internet Explorer')
{
    setClassAttribute = function(e, value) {
        e.setAttribute('className', value);
    }
}
else
{
    setClassAttribute = function(e, value) {
        e.setAttribute('class', value);
    }
}

Now you can use this new function like so:

setClassAttribute(ElementServer, 'server');
Aaron Digulla
A: 

Never use browser detection! This will break in IE8 in Standards Mode!

There is no reason to use setAttribute in this case. e.className = 'some_class' will work in all browsers.

RoToRa