0

How can test the browser and run my function only for firefox?

function myFunc(){
console.log('this should be run only on firefox browser');
}

I came to know jquery.browser is deprecated and says use support instead so I tried $.support.mozilla not work. So how can I do it with support method?

Navin Rauniyar
  • 8,475
  • 10
  • 40
  • 66

3 Answers3

0

Check with this

$.browser.mozilla && $.browser.version > '2' (deprecated)

So use

$.browser.version < '1.9'

Note

Because $.browser uses navigator.userAgent to determine the platform, it is vulnerable to spoofing by the user or misrepresentation by the browser itself. It is always best to avoid browser-specific code entirely where possible. Instead of relying on $.browser it's better to use libraries like Modernizr.

Will not work in jQuery 1.9 or later unless the jQuery Migrate plugin is included.

Dimag Kharab
  • 4,218
  • 1
  • 18
  • 41
0

If i would have to build something like this, i would use this:

if (navigator.userAgent.search("Firefox") >= 0) {
    alert("Jups, is FireFox");
} else { 
    alert("Nope, not FireFox");   
}

Here you go, i set up a Fiddle: http://jsfiddle.net/veritas87/A47tb/1/

Barry Meijer
  • 730
  • 4
  • 16
0

You can use navigator agent:

if(navigator.userAgent.toLowerCase().indexOf('firefox') > -1)
{
     function myFunc(){
         console.log('this should be run only on firefox browser');
     }
}
Felix
  • 36,929
  • 7
  • 39
  • 54