• ベストアンサー

JavaScriptでブラウザの判別をしたいのですがIEだけelseの処理がなぜか追加されてしまう。

ブラウザとバージョンを表示するスクリプトを書きたいのですがIEのときだけ 「Internet Explorer: 6.0不明」のようにelseの部分を書き出してしまいます。 Firefoxのときは「Firefox: 1.5.0.4」となりelseの部分を書き出しません。 なぜこのような結果になるのでしょうか? ちなみにFirefoxの判別の部分を消去するとなぜかこの問題が起こりません。 改善策をご存知の方がおられましたらご回答をよろしくお願いします。 ua = navigator.userAgent; if( ua.indexOf( "MSIE" ) > -1 ) { str = ua.indexOf( "MSIE" ) + 5; ver = ua.substring( str, str + 3 ); document.write( "Internet Explorer: " + ver ); } //↓ここの間を消去すると問題は起こらない。 if( ua.indexOf( "Firefox" ) > -1 ) { str = ua.indexOf( "Firefox" ) + 8; ver = ua.substring( str, str + 7 ); document.write( "Firefox: " + ver ); } //↑ここの間を消去すると問題は起こらない。 else { document.write( "不明" ); }

質問者が選んだベストアンサー

  • ベストアンサー
  • yambejp
  • ベストアンサー率51% (3827/7415)
回答No.1

そりゃあフローで、firefoxじゃないときは 「不明」と書けとなっているからでしょ? ieじゃなく、firefoxじゃないときは不明なら elseがぬけてます ×if(IE){};if(FOX){}else{} ○if(IE){}else if(FOX){}else{}

nana_watuki
質問者

お礼

ご回答いただきどうもありがとうございました。

その他の回答 (1)

  • Ikonos00
  • ベストアンサー率28% (86/302)
回答No.2

if( ua.indexOf( "Firefox" ) > -1 ) を else if( ua.indexOf( "Firefox" ) > -1 ) にすれば良いのでは? 今の状態だと、 ・IEだったら ・FireFoxだったら、それ以外だったら。 と、別々の判定になっていますよ。

nana_watuki
質問者

お礼

ご回答いただきどうもありがとうございました。

関連するQ&A

  • javascriptで別ページに・・・ 続編

    こんにちは。 前回質問させて頂いた事で、特定のユーザーエージェントからのアクセスを任意の別ページに飛ばす方法に成功しました。 下記がそのソースです。 このソースの例では、Internet ExplorerからのアクセスをヤフージャパンTOPページに飛ばす方法です。 ------------------- <script type="text/javascript"> <!-- document.write('<p>ブラウザの判別:'); if(navigator.userAgent.indexOf("Opera") != -1){ // 文字列に「Opera」が含まれている場合 document.write('あなたのブラウザは Opera ですね?'); } else if(navigator.userAgent.indexOf("MSIE") != -1){ // 文字列に「MSIE」が含まれている場合 location.href="http://www.yahoo.co.jp/"; } else if(navigator.userAgent.indexOf("Firefox") != -1){ // 文字列に「Firefox」が含まれている場合 document.write('あなたのブラウザは Firefox ですね?'); } else if(navigator.userAgent.indexOf("Netscape") != -1){ // 文字列に「Netscape」が含まれている場合 document.write('あなたのブラウザは Netscape ですね?'); } else if(navigator.userAgent.indexOf("Safari") != -1){ // 文字列に「Safari」が含まれている場合 document.write('あなたのブラウザは Safari ですね?'); } else{ document.write('判別できませんでした。'); } document.write('</p>'); // --> </script> ----------------------------------- これに対して追加質問させて頂きます。 (1)ユーザーエージェントの指定が上記のままではInternet Explorer利用者の全員が対象になってしまうので、さらにUAを絞り込んで、 例 Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 6.1; SV1) ↑この様に、より長く具体的に絞り込んでUAを指定する事は不可能でしょうな。 自己のアクセス解析で得られた情報を記述してテストしましたが、成功しませんでした。 半角や全角の違いか?それともスペースに問題があるのか分かりません。 (2)上記の例では、ユーザーエジェントで判定しておりますが、ホストで判定する事は不可能でしょうか。 自己の場合の「softbank***********.bbtec.net」でテストしましたが、やはり成功しませんでした。 お手数ですが宜しくお願いします。

  • ブラウザ判別スクリプト

    ブラウザ別にcssを読み込みたいのですが、 firefox2.0に反映されません。 どうしてでしょうか? <!--ブラウザ判別 function getOSType() { var uAgent = navigator.userAgent.toUpperCase(); if (uAgent.indexOf("MAC") >= 0) return "MacOS"; if (uAgent.indexOf("WIN") >= 0) return "Windows"; if (uAgent.indexOf("X11") >= 0) return "UNIX"; return ""; } // Netscape Navigator -> Netscape // Internet Explorer -> Explorer // Safari -> Safari // Opera -> Opera function getBrowserName() { var aName = navigator.appName.toUpperCase(); var uName = navigator.userAgent.toUpperCase(); if (uName.indexOf("SAFARI") >= 0) return "Safari"; if (aName.indexOf("MICROSOFT") >= 0) return "Explorer"; if (uName.indexOf("FIREFOX") >= 0) return "Firefox"; return ""; } function getBrowserVersion() { var browser = getBrowserName(); var version = 0; var s = 0; var e = 0; var appVer = navigator.appVersion; var uName = navigator.userAgent.toUpperCase(); if (browser == "Safari") { version = eval(appVer.substring(0,3)) - 4; } if (browser == "Explorer") { appVer = navigator.userAgent; s = appVer.indexOf("MSIE ",0) + 5; e = appVer.indexOf(";",s); version = eval(appVer.substring(s,e)); } if (browser == "Firefox") { s = uName.indexOf("FIREFOX/",0); version = parseFloat(uName.substring(s+8,s+8+3)); } return version; } //--> <!--top.js os = getOSType(); browser = getBrowserName(); version = getBrowserVersion(); if (os == "MacOS") dirName = "css/mac/"; if (os == "Windows") dirName = "css/win/"; if (os == "UNIX") dirName = "css/unix/"; if (browser == "Explorer") dirName += "ie"; if (browser == "Safari") dirName += "saf"; if (browser == "Firefox") dirName += "ff"; if ((version >= 1) && (version < 2)) dirName += "top.css"; if ((version >= 2) && (version < 3)) dirName += "top.css"; if ((version >= 3) && (version < 4)) dirName += "top.css"; if ((version >= 4) && (version < 5)) dirName += "top.css"; if ((version >= 5) && (version < 5.5)) dirName += "top.css"; if ((version >= 5.5) && (version < 6)) dirName += "top.css"; if ((version >= 6) && (version < 7)) dirName += "top.css"; if ((version >= 7) && (version < 8)) dirName += "top.css"; document.write("<link rel='stylesheet' href='"+dirName+"' type='text/css'>"); //-->

  • 【JS】ブラウザバージョン別分岐がうまくいかない

    UAを使いバージョン別にブラウザを分岐させたいのですが、 『xx以上oo未満』がうまくいきません。 シンタックスはないのですが、 どのような式にしたらいいでしょうか。 script http://the-design-world.com/test/jquery.viewmodechanger.js 該当部分 ... }else if(ua.indexOf("Firefox")>-1){ if(ua.indexOf("Firefox >= 3.5")>-1){ c = 3; }else if((ua.indexOf("Firefox <= 3.6")>-1) && (ua.indexOf("Firefox > 4")>-1)){ c = opt.firefox36; }else if(ua.indexOf("Firefox <= 4")>-1){ c = opt.firefox4upper; }else { c = 1; } ... どなたかご助力いただけますでしょうか。

  • ブラウザの種類によって、処理を分岐。

    ブラウザの種類によって、処理を分岐。 以下のような感じで、ブラウザの種類(SAFARI、OPERA、FIREFOX、EXPLORERなど)によって、 perlの処理を分けたいのですが、やり方がわかりません。 (もちろん、以下は、正常に動作しません。) //↓=========================================== <script language="JavaScript"> <!-- window.onload = function(){ str = navigator.appName.toUpperCase(); if (str.indexOf("SAFARI") >= 0) browser_Name = "SAFARI"; if (str.indexOf("NETSCAPE") >= 0) browser_Name = "NETSCAPE"; if (str.indexOf("OPERA") >= 0) browser_Name = "OPERA"; if (str.indexOf("FIREFOX") >= 0) browser_Name = "FIREFOX"; if (str.indexOf("EXPLORER") >= 0) browser_Name = "EXPLORER"; if (str.indexOf("MICROSOFT") >= 0) browser_Name = "EXPLORER"; if (browser_Name == "SAFARI"){ EOM $a="1"; print <<EOM; } if (browser_Name == "NETSCAPE"){ EOM $a="10"; print <<EOM; }  : } // --> </script> //↑=========================================== どうかご教授願います。よろしくお願いします。

    • ベストアンサー
    • Perl
  • 複数ブラウザ対応のお気に入りに追加ボタンを設置したのですが

    複数ブラウザ対応のお気に入りに追加ボタンを設置したのですが マウスポインタを乗せても矢印アイコンから人差し指のアイコンに変化せず困っています。 矢印のままクリックしても一応機能はするのですがやはり違和感があります。 ソースはこちらです。 <script type="text/javascript"> <!-- //Internet Explorer //ブラウザ上では[お気に入りに追加♪]というボタンが表示されます if(navigator.userAgent.indexOf("MSIE") > -1){ document.write('<div><img src="画像URL" border="0" alt="お気に入りに追加"'); document.write(' onclick="window.external.AddFavorite(\'サイトURL\',\'サイトタイトル\')"></div>'); } //Firefox //ブラウザ上では[ブックマークに追加♪]というボタンが表示されます else if(navigator.userAgent.indexOf("Firefox") > -1){ document.write('<div><img src="画像URL" border="0" alt="お気に入りに追加"'); document.write(' onclick="window.sidebar.addPanel(\'サイトタイトル\',\'サイトURL\',\'\');"></div>'); } //Netscape //ブラウザ上では[ブックマークに追加♪]というボタンが表示されます else if(navigator.userAgent.indexOf("Netscape") > -1){ document.write('<div><img src="画像URL" border="0" alt="お気に入りに追加"'); document.write(' onclick="window.sidebar.addPanel(\'サイトタイトル\',\'サイトURL \',\'\');"></div>'); } //Opera //ブラウザ上では[ブックマークに追加♪]というリンクが表示されます else if(navigator.userAgent.indexOf("Opera") > -1){ document.write('<div><a href="サイトURL" rel="sidebar" title="サイトタイトル"><img src="画像URL" border="0"></a></div>'); } //上記以外のブラウザは、何も実行(表示)されません </script> もともとのソースから多少改変してありますがJAVAの知識が全くないもので ソースを改良していただけると助かります。

  • indexOf !=-1の意味

    Javascriptのブラウザ判別のコードで分からない所があります。 以下のindexOf !=-1 の部分ですが、indexOfメソッドで文字列を検索した値がマイナス1ではない時というのはどういう意味ですか? ブラウザが何か情報を持っているのでしょうか? -1は何々、0は何々、1は何々と。 <!DOCTYPE html> <html><head> ~省略~ <title></title> <style type="text/css"> <!-- body { background-color: #ffffff; } --> </style> </head> <body> <p> <script type="text/javascript"> <!-- if(Firefox_Moz){ if(UA.indexOf("Firefox") != -1){document.write("Firefox")} else{document.write("Mozilla")} } //--> </script> </p> </body></html>

  • Javascriptでブラウザ判別、iframe内の子要素にscrollingの指定をするには?

    Javascript初心者です。いろいろ探したのですが解決方法がわかりません。 index.htmlにiframe(name=""main)があり、それぞれの子要素にtarget="main"で指定しているHPを作成したのですが、子の要素にカスタマイズスクロールバー(flexcroll http://youmos.com/news/flexcroll_js )を取り入れています。 ※ちなみに上記iframeはindex.html内でサイズやリンク先などを指定しているのですが、そこでscrolling="no"にしています(子のflexscrollと重複するので)。 MacIEとSafariではスクロールバーが表示されません。(Winはfirefox、IE6、Opera9、Netscape7.1、Macではfirefoxはflexcroll表示確認済みです。) その不具合の対処にindexのhead内に<script type="text/javascript" src="../js/distinction.js"></script>にブラウザ判別をさせて、 MacIEとSafariには別のjsファイルを適用させ、flexcrollを無効にし、デフォルトのスクロールバーが出るように指定したいのですが、 上記※にあるとおり、index内でiframeのscrollingをnoにしているため、どうしたらいいのかわからなくて困っています。 下記がdistinction.js内のスクリプトです(調べながら作成したので自信がありません)。 // JavaScript Document if(navigator.userAgent.indexOf('Gecko/')!=-1) { document.write('<script language="javascript" src="js/flexcroll.js"></scr'+'ipt>\n'); } else if (document.window.clipboardData) { document.write('<script language="javascript" src="js/flexcroll.js"></scr'+'ipt>\n'); } else { document.write('<script language="javascript" src="js/macIE.js"></scr'+'ipt>\n'); } } else if (window.opera) { document.write('<script language="javascript" src="js/flexcroll.js"></scr'+'ipt>\n'); } else if(navigator.userAgent.indexOf('AppleWebKit')!=-1) { document.write('<script language="javascript" src="js/macsafari.js"></scr'+'ipt>\n'); } else { document.write('<script language="javascript" src="js/other.js"></scr'+'ipt>\n'); } // --> また、flexcroll.jsを適用しないもののjsファイルの中身もなんと記述したらいいのかわかりません。そもそも、この対処法であっているのでしょうか? 間違いや解決方を教えてください。

  • !!winVar

    サブウィンドウの有無確認関数の中で「!!winVar」という記述がありますが、 これはいったい何を意味しているのでしょうか? またソースの内容もいまいち理解できません。 細部の意味について知っておられる方がいましたら、 御教授のほど宜しくお願いします。 //子ウインドウ有無確認関数 function sbwin_closed(winVar) { var ua = navigator.userAgent if( !!winVar ) if( ua.indexOf('MSIE 4')!=-1 && ua.indexOf('Win')!=-1 ) return winVar.closed else return typeof winVar.document != 'object' else return true }

  • SafariのIframeで高さが取得できません

    <script language="JavaScript"> <!-- function frame_check(){ if(main.document.body){ if(navigator.userAgent.indexOf("Opera") != -1){ // 文字列に「Opera」が含まれている場合 var f = main.document.body.scrollHeight; } else if(navigator.userAgent.indexOf("MSIE") != -1){ // 文字列に「MSIE」が含まれている場合 var f = main.document.body.scrollHeight; } else if(navigator.userAgent.indexOf("Firefox") != -1){ // 文字列に「Firefox」が含まれている場合 var f = main.document.body.offsetHeight; } else if(navigator.userAgent.indexOf("Netscape") != -1){ // 文字列に「Netscape」が含まれている場合 var f = main.document.body.offsetHeight; } else if(navigator.userAgent.indexOf("Safari") != -1){ // 文字列に「Safari」が含まれている場合 var f = main.documentElement.offsetHeight; } if(f) document.getElementById("frame_set").height=f+20; setTimeout("frame_check()",100); }else{ setTimeout("frame_check()",100); } } setTimeout("frame_check()",10); --></script> <iframe name="main" id="frame_set" src="mainf.html" width="800" height="4000" frameborder="0" scrolling="no"></iframe> 上記のスクリプトを作成しましたが、IE7、 Firefoxではうまくいきますが、SafariではIframeに表示するページの高さが取得できません。 どこが問題なのかご教授お願いいたします。

  • ブラウザで表示を変更

    <SCRIPT TYPE="text/javascript"> <!-- if (navigator.userAgent.indexOf("MSIE")!="-1"){//for IE document.write("インターネットエクスプローラー"); } else { document.write("別ブラウザ"); } //--> </SCRIPT> 上記JavaScriptと同じように、インターネットエクスプローラーとそれ以外のブラウザで表示させるメッセージを変えたいのですが、どのように書けばいいのでしょうか。

    • ベストアンサー
    • Perl

専門家に質問してみよう