navigator.appNameによるブラウザ識別実験

1.Internet Explorer であることのチェック

Internet Explorerの場合、navigator.appNameに「Microsoft」の文字が含まれています。
よって、この文字が含まれていればInternet Explorerであると判断できます。
// 「Microsoft」の文字列チェック
if (navigator.appName.indexOf("Microsoft") > -1)
  document.write("IEです。");
else
  document.write("IEではありません。");
実行結果:


2.Netscape Navigatorであることのチェック

Netscape Navigatorの場合、navigator.appNameは「Netscape」が返されます。
よって、この文字が含まれていればNetscape Navigatorであると判断できます。
// 「Netscape」の文字列チェック
if (navigator.appName.indexOf("Netscape") > -1)
  document.write("NNです。");
else
  document.write("NNではありません。");
実行結果:


3.変数に記憶する方法

目的のブラウザであるかどうかを、あらかじめ変数に記憶させておけば、必要な箇所でif文で簡単にチェックできます。
IEチェックを変数に記憶した場合
// IEかどうかを変数(isIE)にメモ
var isIE = navigator.appName.indexOf("Microsoft") > -1;
// 変数の内容を表示
document.write("isIE = " + isIE + "<br>");
// if文に使用する
if (isIE)  document.write("Internet Explorer です。<br>");
else  document.write("Internet Explorer ではありません。<br>");
実行結果:

NNチェックを変数に記憶した場合
// NNかどうかを変数(isNN)にメモ
var isNN = navigator.appName.indexOf("Netscape") > -1;
// 変数の内容を表示
document.write("isNN = " + isNN + "<br>");
// if文に使用する
if (isNN)  document.write("Netscape Navigator です。<br>");
else  document.write("Netscape Navigator ではありません。<br>");
実行結果:

メイン画面に戻る