根據userAgent實現跳轉到移動或者pc頁面程式碼

admin發表於2017-04-12

可以根據瀏覽器的userAgent的資訊來實現選擇移動端還是pc端頁面的跳轉功能。

程式碼例項如下:

[JavaScript] 純文字檢視 複製程式碼
var ua = window.navigator.userAgent;
if (ua.match(/mobile/i)) {
  location.href = 'http://m.baidu.com';
} else {
  location.href = 'http://www.baidu.com';
}

相關文章