この記事では、モバイルアクセスデバイスを決定し、対応するCSSを解析するJavaScriptの方法について説明します。参照のためにそれを共有してください。特定の分析は次のとおりです。
JavaScriptは、主にモバイルデバイス用にさまざまなWebアクセス環境を決定し、対応する分析ソリューションを提供します(デバイスコードを直接コピーするデバイスコードがネットワークを直接コピーします)。
コードを次のようにコピーします。
if(/applewebkit.*mobile/i.test(navigator.useragent)|| (/midp | symbianos|nokia|samsung| lg|nec|tcl| alcatel|bird| dbtel| dopod| philips| haier| lenovo| mot-mot-nokia-nokiasson|sie- amoi| zte/.test(navigator.useragent)){
if(window.location.href.indexof( "?mobile")<0){
試す{
if(/android|webos|iphone|ipod|blackberry/i.test(navigator.useragent)){
//アクセス環境がAndroid | webos | iPhone | iPod | BlackBerryであると判断してから、次のスタイルをロードします
setactivestylesheet( "style_mobile_a.css");
}
else if(/pad/i.test(navigator.useragent)){
//アクセス環境がiPadであると判断した場合は、次のスタイルをロードします
setactivestylesheet( "style_mobile_ipad.css");
}
それ以外{
//アクセス環境が別のモバイルデバイスであると判断した場合、次のスタイルがロードされます
setactivestylesheet( "style_mobile_other.css");
}
}
catch(e){}
}
}
それ以外{
//上記のいずれもロードされていない場合、次のスタイル
setactivestylesheet( "style_mobile_no.css");
}
//判断後のスタイルの読み込み
function setactivestylesheet(filename){document.write( "<link href ="+filename+"rel = styleSheet>");}
読み込みページ:
次のようにコードをコピーします。<script type = "text/javascript">
if(/applewebkit.*mobile/i.test(navigator.useragent)|| (/midp | symbianos|nokia|samsung| lg|nec|tcl| alcatel|bird| dbtel| dopod| philips| haier| lenovo| mot-mot-nokia-nokiasson|sie- amoi| zte/.test(navigator.useragent)){
if(window.location.href.indexof( "?mobile")<0){
試す{
if(/android|webos|iphone|ipod|blackberry/i.test(navigator.useragent)){
window.location.href = "モバイルページ";
} else if(/pad/i.test(navigator.useragent)){
window.location.href = "tablet page";
}それ以外{
window.location.href = "その他のモバイルページ"
}
} catch(e){}
}
}
</script>
この記事がみんなのJavaScriptプログラミングに役立つことを願っています。