Documentation and Books

Recent site activity

Web Development‎ > ‎JavaScript‎ > ‎

How to detect what type of browser is used?

This is a sample that can detect if you are working on IE, Netscape, Firefox or Opera, where all the the browsers it also checks which version except Firefox:

    // Internet Explorer

  var ie = document.all != null; //ie4 and above
var ie5 = document.getElementById && document.all;
var ie6 = document.getElementById && document.all&&(navigator.appVersion.indexOf("MSIE 6.")>=0);

// IPhone
var iphone = browser.indexOf('iPhone') != -1; 
    // Netscape
  var ns4 = document.layers != null;
var ns6 = document.getElementById && !document.all;
var ns = ns4 || ns6;

// Firefox
var ff = !document.layers && !document.all;

// Opera
var op = navigator.userAgent.indexOf("opera")>0;
var op7 = op && operaVersion() <= 7;
var op8 = op && operaVersion() >= 8;

// Detects the Opera version
function operaVersion() {
agent = navigator.userAgent;
idx = agent.indexOf("opera");
if (idx>-1) {
return parseInt(agent.subString(idx+6,idx+7));
}
}