簡體   English   中英

如何檢測桌面和移動Chrome用戶代理?

[英]How do you detect between a Desktop and Mobile Chrome User Agent?

對於Chrome桌面擴展主頁,我正在嘗試檢測用戶是否在Android上使用Chrome for Desktop或Chrome for Mobile。 目前,下面的腳本將Android Chrome與Desktop chrome相同。 在桌面Chrome上,它應該顯示“chrome”鏈接; 但是,如果有人在Chrome for Android上,它應該顯示“移動其他”鏈接。

腳本:

<script>$(document).ready(function(){
    var ua = navigator.userAgent;
    if (/Chrome/i.test(ua))
       $('a.chrome').show();

    else if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile/i.test(ua))
       $('a.mobile-other').show();

    else
       $('a.desktop-other').show();
  });</script>

Chrome Android用戶代理:

Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev> (KHTML, like Gecko) Chrome/<Chrome Rev> Mobile Safari/<WebKit Rev>

問題是用戶代理將始終擁有“Chrome”,無論是桌面版還是移動版。 所以你必須先檢查更具體的案例。

$(document).ready(function(){
    var ua = navigator.userAgent;

    if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile|CriOS/i.test(ua))
       $('a.mobile-other').show();

    else if(/Chrome/i.test(ua))
       $('a.chrome').show();

    else
       $('a.desktop-other').show();
});

因此,要根據最新的Chrome for iOS用戶代理字符串更新@ imtheman的代碼:

$(document).ready(function(){
var ua = navigator.userAgent;

if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile|CriOS/i.test(ua))
   $('a.mobile-other').show();

else if (/Chrome/i.test(ua))
   $('a.chrome').show();

else
   $('a.desktop-other').show();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM