|
|
|||||
|
Ready to Use Script Archive | | | Over 5000 Free Fonts | | | Tutorials | | | Javascript Forum | | | Other Javascript Resources | | | Cheat Sheet |
Browser NameQuestion: How do I detect the browser name (vendor)?
Answer:
To distinguish between the two major browsers, you can use the
Thus, to implement a Netscape/Microsoft code fork, you can use the following combination of conditional operators: if (navigator.appName=="Netscape") { // Netscape-specific code goes here } if ((navigator.appName).indexOf("Microsoft")!=-1) { // Internet Explorer-specific code goes here }Note, however, that for compatibility with Netscape Navigator, Opera browsers also return the string "Netscape"
as the value of navigator.appName .
To distinguish between genuine Netscape browsers and Opera,
you can use the navigator.userAgent property:
the value of navigator.userAgent
contains a substring "Opera" only for Opera browsers.
The code below implements the Netscape/Opera/Microsoft browser detection: document.write('navigator.appName = '+navigator.appName+'<br>') document.write('navigator.userAgent = '+navigator.userAgent+'<br>') if ((navigator.userAgent).indexOf("Opera")!=-1) { document.write("You are using an Opera browser.") } else if (navigator.appName=="Netscape") { document.write("You are using a Netscape browser.") } else if ((navigator.appName).indexOf("Microsoft")!=-1) { document.write("You are using Microsoft Internet Explorer.") } In your browser, this code produces the following output:
|
|