userAgent 屬性是一個只讀的字符串,聲明了瀏覽器用于 HTTP 請求的用戶代理頭的值要糊。
function isMobile () {
????var ua = navigator.userAgent.toLowerCase();
????var StringPhoneReg =? "\\b(ip(hone|od)|android|opera m(ob|in)i"
????????+ "|windows (phone|ce)|blackberry"
????????+ "|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp"
????????+ "|laystation portable)|nokia|fennec|htc[-_]"
????????+ "|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b"; var StringTableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser"
????????+ "|[1-4][0-9]{2}x[1-4][0-9]{2})\\b"; console.log(ua);
????var isIphone = ua.match(StringPhoneReg),
????isTable = ua.match(StringTableReg),
????isMobile = isIphone || isTable;
????if (isMobile) {
? ? ? ? var lcation = 'http://' + window.location.host + '/m
????????window.location.href = lcation;
????????return true;
????} else {
? ? ? ? var location = 'http://' + window.location.host
? ? ????window.location.href = location;
????????return false;
????}
}
最后在<script type="text/javascript"></script>里添加onload事件調(diào)用即可