2012-05-04 3 views
2

Googleのインデックスページには、スマートフォン(iPhone、Android & Windows Phone)のユーザーをモバイルサイトにリダイレクトするスクリプトがあります。我々が使用するテクニックは次のとおりです。IE9が誤ってモバイルサイトにリダイレクトされる

if (navigator.userAgent.match(/iPhone/) && ! navigator.userAgent.match(/iPad/)) { 
     window.location="mobile.html"; 
     } 
    else if (navigator.userAgent.match(/Android/) && ! navigator.userAgent.match(/Android 3/)) { 
     window.location="mobile.html"; 
     } 
    else if (navigator.userAgent.match(/Windows Phone/) || navigator.userAgent.match(/Zune/)) { 
     window.location="mobile.html"; 
     } 

我々はいくつかの理由でそのuserAgentには、上記のいずれかの文字列が含まれていないにもかかわらず、モバイルサイトにリダイレクトされIE9上でこれをテストするまで、すべてが完璧に働いていました。 IE9のためのuserAgentには、次のとおりです。

のMozilla/5.0(互換; MSIE 9.0; Windows NTの6.1; WOW64;トライデント/ 5.0)

IE8がこのように振る舞うや他のプラットフォームをしていません。スクリプトが間違っているのでしょうか、それとも復讐したことでもう一度IEがストライキしましたか?ありがとう。

+0

のために作成した実際のディレクトリにwindow.locationの=「mobile.html」に変更

if(/iPhone/.test(navigator.userAgent) && !/iPad/.test(navigator.userAgent)){ window.location="/mobile"; }else if(/Android/.test(navigator.userAgent) && !/Android 3/.test(navigator.userAgent){ window.location="/mobile"; }else if(/Windows Phone/.test(navigator.userAgent) || /Zune/.test(navigator.userAgent)){ window.location="/mobile"; } 

のWindows 8の電話を使用して私のために働きましたコードをデバッグすると、どちらが落ちているのか分かりますか? – Adam

+0

@RMX - 私はアダムに同意します。各制御構造で 'console.log(navigator.userAgent);'を実行して、どの条件が一致しているのか、そしてどのuserAgentが報告されているのかを確認してください。 – jaredhoyt

答えて

2
を返しますので、それは働くつもりだと思います

このpost regarding IE9 User Agentsによると、IE9は互換モードではユーザーエージェントを変更し、 'Zune'文字列を含むことができます。 Windows Phoneユーザーをリダイレクトするために別の文字列を使用する必要があるようです。ありがとう。

+0

する必要はありません。正しいdoctypeを指定して、IE9が互換モードにならないようにしてください。 –

1

.match()メソッドを使用すると、配列は空です([])。
IEはprobrably []true表現であることを考慮されたので、私はあなたが次の操作を行うことをお勧め:

if(/iPhone/.test(navigator.userAgent) && !/iPad/.test(navigator.userAgent)){ 
    window.location="mobile.html"; 
}else if(/Android/.test(navigator.userAgent) && !/Android 3/.test(navigator.userAgent){ 
    window.location="mobile.html"; 
}else if(/Windows Phone/.test(navigator.userAgent) || /Zune/.test(navigator.userAgent)){ 
    window.location="mobile.html"; 
} 

私は.TEST()メソッドのみtrueまたはfalse

+0

IE9は依然としてスクリプトを使用してモバイルサイトにリダイレクトしています。私たちはスクリプトの最後の行( 'Windows Phone'と 'Zune')を削除し、IE9はリダイレクトしません。また、IE9 Developers ToolsのuserAgentがDefaultに選択されていて、他に何も変更されていないことを確認しました。 – RMX

0

ご回答ありがとうございます。以下は、私は私があなたの場合は、モバイルのindex.htmlファイル

ex. /root directory/mobile