用 User Agent 判斷移動設備
WebApp除了做成響應式設計以外,還可以給移動設備做一套UI,PC端再做一套UI,然后在前台進行判斷進行相應的跳轉。判斷是否是移動設備一般根據瀏覽器的useragent進行判斷,雖然可以偽造,但是用起來方便,和Chrome的設備模擬功能配合起來
調試方便。
代碼
//根據useragnet判斷是否是移動設備
function isMobileDevice(ua) {
if (/(iphone|ios|android|mini|mobile|mobi|Nokia|Symbian|iPod|iPad|Windows\s+Phone|MQQBrowser|wp7|wp8|UCBrowser7|UCWEB|360\s+Aphone\s+Browser)/i.test(ua)) {
return true;
}
return false;
}
$(document).ready(function () {
var useragent = navigator.userAgent;
if (isMobileDevice(useragent)) {
window.location.href = "/Home/MobileIndex";
return;
}
});