如何使用Javascript添加iPhone/iPad检测?

Cri*_*tel 1 javascript mobile jquery browser-detection

我想在我的网站上添加一个条件,如果浏览器来自iPhone/iPad,有一些特定的行为,比如单击一个按钮来显示div.

就像是 :

<script type="text/javascript">
/* iPhone scripts */
<?php if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod')){ ?>
    // window.addEventListener("load",function() {
    // setTimeout(function(){
    // window.scrollTo(0, 1);
    // }, 0);
    // });
   jQuery(document).ready(function() {
}
});
Run Code Online (Sandbox Code Playgroud)

这正是我发现的,但我不确定这是怎么做的.

ins*_*ere 6

我用的是这个:

function UtilityHasTouch() {    

    var agent   = navigator.userAgent;

    if ( agent.match(/(iPhone|iPod|iPad|Blackberry|Android)/) ) {
        return true;
    }

    try {
        document.createEvent("TouchEvent");
        return true;
    } catch (e) {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)