How can I display different content depending on whether the browser is Internet Explorer or Netscape?
JavaScript has a built-in object callednavigator
that can be used to obtain information about the user's web browser. The navigator
object has several properties, a few of which are described below:
property name* | description | value on your browser |
---|---|---|
appCodeName | code name of browser | |
appName | name of browser | |
appVersion | version information about browser | |
userAgent | value of user-agent header sent by browser during HTTP requests |
Your JavaScript code can use these properties to determine whether the user is using Netscape Navigator or Internet Explorer. For example, suppose that you have optimized your page for viewing on Netscape, and you would like to serve warning to users of the "other" browser. You can accomplish this with the following code:
If you place this code betweenif (navigator . appName != "Netscape") alert ("Please use the Netscape Navigator to browse this site.");
<script>
and </script>
tags in the head of your document, users who visit your site with JavaScript-enabled browsers other than Netscape will be greeted by an alert box instructing them to use Netscape.
If you just want to show document content that varies depending on the browser, use conditional document . write
statements. For example, the following HTML/JavaScript code displays
Thank you for using Netscape.on the Netscape Navigator, or
Please use Netscape Navigator.on any other browser, such as Internet Explorer:
You can also use the tags
<noscript>
and </noscript>
to display text on browsers that do not support JavaScript, since JavaScript-enabled browsers ignore HTML content enclosed by these tags.
Further information about the navigator
object, its properties, and its methods can be found in Netscape Communication's JavaScript Authoring Guide.
One last note: If you use the navigator
object to distinguish between Netscape Navigator and Internet Explorer, be warned that Explorer may pretend to be Netscape. On Internet Explorer 3.0 for Windows 95, for example, the expression
navigator . codeName
returns
Mozilla/2.0 (compatible; MSIE 3.0A; Windows 95),revealing the scary fact that when Explorer asks web servers for documents, it tries to convince the servers that it is a Netscape browser. I guess some browsers will always be wanna-be's. :-)
Charlton Rose
12/12/96