使用JavaScript检测iPhone和iPod

    技术2022-07-11  131

    I was browsing the ESPN.com source code recently and stumbled upon their method of detecting iPhone / iPod touch users and redirecting them to their iPhone-compatible website.

    我最近在浏览ESPN.com源代码,偶然发现了他们检测iPhone / iPod touch用户并将其重定向到其iPhone兼容网站的方法。

    JavaScript (The JavaScript)

    if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) { if (document.cookie.indexOf("iphone_redirect=false") == -1) window.location = "http://m.espn.go.com/wireless/?iphone&i=COMR"; }

    The code itself is short and sweet. I, however, would prefer using the server-side method of user agent checking.

    代码本身简短而甜美。 但是,我更喜欢使用服务器端的用户代理检查方法。

    PHP (The PHP)

    if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod')) { header('Location: http://yoursite.com/iphone'); exit(); }

    Which would you prefer?

    你更喜欢哪个?

    翻译自: https://davidwalsh.name/detect-iphone

    相关资源:jdk-8u281-windows-x64.exe
    Processed: 0.014, SQL: 9