0

Trying to show and hide js only on Firefox browser. There are another question about Detect all Firefox versions, but it is not answer of my question so i have asked this question.

HTML:

<div id="about_me">
This Text color will be change
</div>

Show this only on Firefox:

$("#about_me").addClass("red");

I have trying this but not working:

<!--[if Gecko ]>
$("#about_me").addClass("red");
<![endif]-->

And Show this to other browsers and hide on Firefox:

$("#about_me").addClass("blue");

How show JS to different browser, that text color will be red only on Firefox and blue on other browsers.

Please See this Fiddle >>
Thanks.

Community
  • 1
  • 1
Aariba
  • 1,174
  • 5
  • 20
  • 52

3 Answers3

2

Try with navigator.userAgent used to detect the browser

The userAgent property returns the value of the user-agent header sent by the browser to the server.

The value returned, contains information about the name, version and platform of the browser.

if(navigator.userAgent.toLowerCase().indexOf("firefox") > -1){
    $("#about_me").addClass("red");
}
else{    
    $("#about_me").addClass("blue");    
}

Fiddle

About Naviagate Useragent | MDN

Note : Run the fiddle in all browsers. I checked in chrome, safari, IE and firefox

Sudharsan S
  • 15,336
  • 3
  • 31
  • 49
1
if(navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
{
$("#about_me").addClass("red");
}

Alternatively you can use like this also

if(navigator.userAgent.indexOf('Firefox') > -1)
{
$("#about_me").addClass("red");
}
Techy
  • 2,626
  • 7
  • 41
  • 88
1

It should work:

HTML:

<br>
<div id="about_me">
     This Text color will be change
</div>e here

CSS:

.red { color: red; }
.blue { color: blue; }

JS:

var userAgent = navigator.userAgent.toLocaleLowerCase();  

if(userAgent.indexOf('firefox') > -1) {
    $("#about_me").addClass("red");
} else {
    $("#about_me").addClass("blue"); 
}

Fiddle

http://jsfiddle.net/pyg9ynb5/2/

  • About the `toLocaleLowerCase()` it's just to lowercase the entire user agent string. Actually, it's not need, you can remove if you want and compare with 'Firefox' on if statement. If it's ok for you, please accept my answer as correct. – Raul Souza Lima Jun 04 '15 at 06:58