if(firefox and is on a computer){
alert('using firefox on a computer')
}else{
alert("using something else!");
}
How can I do this?
if(firefox and is on a computer){
alert('using firefox on a computer')
}else{
alert("using something else!");
}
How can I do this?
You tagged this jquery
, so there is jquery.browser.
Example from the manual page:
jQuery.each(jQuery.browser, function(i, val) {
if(i=="mozilla" && jQuery.browser.version.substr(0,3)=="1.9")
alert("Do stuff for firefox 3")
});
they recommend against checking for specific browsers, though, and say to use feature detection instead.
Use a real library like jquery etc, then you really shouldnt need to worry about this most of the time.
You can use navigator.userAgent
for this. Just see if it contains Mozilla
You can make the control with javascript's navigator.userAgent
or navigator
object in general,
But if you want to use something ready to go, check this:
http://www.quirksmode.org/js/detect.html
hope this helps, Sinan.
It's better to detect features you need, not a browser. For example, if you need to know if foo() is supported, you can check it with if(foo){}
What you're after is known as browser detection:
if ($.browser.mozilla) { ...
However, browser sniffing is discouraged, as its easy to spoof the user agent, i.e. pretend to be another browser!
You'd best use feature detection, either in your own way, or through the jQuery.support
interface: http://api.jquery.com/jQuery.support/
Here's an article on extending it for your own use: http://www.waytoocrowded.com/2009/03/14/jquery-supportminheight/
Edit:
Found this post as well which helps: http://stackoverflow.com/questions/1298713/when-ie8-is-not-ie8-what-is-browser-version/1298738#1298738
navigator.sayswho= (function(){
var N= navigator.appName, ua= navigator.userAgent, tem;
var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
return M.join(' ');
})();
as the name suggests, this is who the browser says it is- but use object detection before asking it to actually do anything...
I use it for logging errors from users and in testing code in multiple browsers- where I know the userAgent strings.
Like this: Check for Firefox. Or some other browser.
window.onload = function() {
// alert(navigator.userAgent);
if (navigator.userAgent.indexOf("Firefox") > 0) {
alert("ff");
}
}
As already asked in a comment: why do you want this? Browser sniffing is a bad habit and there are only a few situations where it is needed.
Instead, use feature detection. As described by Nicholas Zakas, you should test relatively 'uncommon' features before using them and only rely on these tests, so you're kind of fail-safe. For example, do
if (window.XMLHttpRequest)
var xhr = new XMLHttpRequest();
instead of
if ((brwsr.IE && brwsr.IE.version >= 7) || (brwsr.firefox) || (brwsr.opera))
var xhr = new XMLHttpRequest();
And also don't do
if (window.XMLHttpRequest)
// Hey, native XMLHttpRequest-support, so position: fixed is also supported
(instead, test if position: fixed
is supported)
There exist several uncommon browsers with names like Kazehakase and Midori that also might, or might not, support these features, so your scripts will silently work on them when using feature detection.
But please read the mentioned article, as it contains a very good and thorough explanation of this technique. (By the way, I think that Zakas' Professional JavaScript for Web Developers is still too unknown.)
Usually you'd like to separate IE from all the others, because "the others" behave all almost the same way (read: according the standards). The shortest and most reliable way for this is using the IE's conditional compilation feature:
var IE = /*@cc_on!@*/false;
which can be used as
if (IE) {
// IE.
} else {
// Others.
}
In IE, the !
will be compiled and taken in the expression, resulting in a new expression !false
, which is logically true
. We could also use the well known document.all
check here, but this would evaluate to true
in certain Opera versions as well.
If you really, really insist in detecting FireFox (actually: the Gecko/Mozilla engine) only, then just detect if a Gecko/Mozilla specific property exists:
var FF = !(window.mozInnerScreenX == null);
which can be used as
if (FF) {
// FF.
} else {
// Others.
}