As a software developers we must have to deal with javascript every now and than. There are some scripts which is browser dependent. You need to write different functions for different browsers. To achieve this you must require to detect the browser. Below code may helps you,
1: var isIE = false;
2: var isFF = false;
3: var isOP = false;
4: var isSafari = false;
5:
6: function DetectBrowser()
7: {
8: var val = navigator.userAgent.toLowerCase();
9:
10: if(val.indexOf("firefox") > -1)
11: {
12: isFF = true;
13: }
14: else if(val.indexOf("opera") > -1)
15: {
16: isOP = true;
17: }
18: else if(val.indexOf("msie") > -1)
19: {
20: isIE = true;
21: }
22: else if(val.indexOf("safari") > -1)
23: {
24: isIE = true;
25: }
26:
27: }
Fig – (1) Code to detect browser using javascript.
Happy Programming !!!
Guys,
I would like to give my suggestion, please be careful in english spelling and grammer so that information would be easy to understand by anyone in world.
Regards
Muthukumar.P
Northern Ireland, UK
hi,
I read your article of “How to detect browser using JavaScript” its great but i have one question about this js. plz help me.
else if(val.indexOf(“msie”) > -1)
19: {
20: isIE = true;
21: }
22: else if(val.indexOf(“safari”) > -1)
23: {
24: isIE = true;
25: }
in this code if it is “msie” then isIE will get true but if it is “Safari” then also isIE will get true?
Now in market there are 2 versions of IE i.e IE 6.0 and IE 7.0 how we can get these 2 versions by js?
good one
It’s really good !!!
hi,
i think there is typo error in the detect browser code, instead of isSafari = true, it is mentioned as isIE = true.
code error..not working
Nice Code :), Two Thumbs up, Helped a lot !!
Really Nice
What about Chrome?
NOT WORKING CODE IS INCOMPLETE!!!
This is the fixed code:
var isIE = false;
var isFF = false;
var isOP = false;
var isSafari = false;
var val = navigator.userAgent.toLowerCase();
if(val.indexOf(“firefox”) > -1)
{
isFF = true;
}
else if(val.indexOf(“opera”) > -1)
{
isOP = true;
}
else if(val.indexOf(“msie”) > -1)
{
isIE = true;
}
else if(val.indexOf(“safari”) > -1)
{
isSafari = true;
}
This works.
But…after copying and pasting, make sure to replace all OPEN DOUBLE QUOTES (“) and RIGHT DOUBLE QUOTES (”) with ordinary DOUBLE QUOTES (“).
if(navigator.userAgent.toLowerCase().indexOf(“msie”) > -1)
{alert(“IE”);
}else{
alert(“Other Browser”);
}
if(~val.indexOf(“firefox”)){
// is a slightly more awesome way to do it.
}