Personal tools

Detect Browser with JavaScript





Browser detection can be useful for a variety of reasons - to customize styles where the browsers fails to comply with CSS standards, to warn users that they're browser is not suitable for the website they are viewing, or simply to impress a user by showing them how much you know about their browser!

The following JavaScript can be used to detect a user's web browser:

DOM = (document.getElementById) ? 1 : 0;
NS4 = (document.layers) ? 1 : 0;
Konqueror = (navigator.userAgent.indexOf("Konqueror") > -1) ? 1 : 0;
Konqueror21 = (navigator.userAgent.indexOf("Konqueror 2.1") > -1 ||
  navigator.userAgent.indexOf("Konqueror/2.1") > -1) ? 1 : 0;
Konqueror22 = (navigator.userAgent.indexOf("Konqueror 2.2") > -1 ||
  navigator.userAgent.indexOf("Konqueror/2.2") > -1) ? 1 : 0;
Konqueror2 = Konqueror21 || Konqueror22;
Opera = (navigator.userAgent.indexOf("Opera") > -1) ? 1 : 0;
Opera5 = (navigator.userAgent.indexOf("Opera 5") > -1 ||
  navigator.userAgent.indexOf("Opera/5") > -1) ? 1 : 0;
Opera6 = (navigator.userAgent.indexOf("Opera 6") > -1 ||
  navigator.userAgent.indexOf("Opera/6") > -1) ? 1 : 0;
Opera56 = Opera5 || Opera6;
IE = (document.all) ? 1 : 0;
IE4 = IE && !DOM;


The browsers in the code example above serve only as an example. If you wish to support another browser, you can find out how JavaScript represents it using the example below:

alert (navigator.userAgent);


See also Modify CSS with JavaScript

Need assistance with your project? Universal Web Services can help.
Contact us to request a quote.