範例:


<!DOCTYPE html>
<html>

<title>UserAgent</title>


<p>利用UserAgent判斷是哪種行動裝置</p>

<script>
var userAgentContent = "UserAgent內容為:\n"+navigator.userAgent;

//確認UserAgent中是否包含str
function judge(str){
return (userAgentContent.indexOf(str)>0);
}

//如果為iPhone回傳true
function isIPhone(){return judge("iPhone")};

//如果為iPad回傳true
function isIPad(){return judge("iPad")};

//如果為Android回傳true
function isAndroid(){return judge("Android")};

if(isIPhone()){

alert("這是IPhone!!");
}else if(isIPad()){

alert("這是IPad!!");
}else if(isAndroid()){

alert("這是Android!!");
}else{

alert("這不是IPhone/IPad/Android\n請看詳細資料\n"+userAgentContent);
}

</script>
</html>


 

 


arrow
arrow
    文章標籤
    UserAgent html判斷瀏覽器
    全站熱搜

    cookiesp 發表在 痞客邦 留言(0) 人氣()