時下,許多web網站都有自己的手機建站,當用行動裝置瀏覽網站的時候,我們希望能跳轉到手機網址上,下面是一套並不是很完善的代碼,高手可繼續完善。
代碼貼出:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>js判斷裝置類型是行動裝置還是PC裝置</title><script type="text/javascript">// borwserRedirect(function browserRedirect(){var sUserAgent = navigator.userAgent.toLowerCase();var bIsIpad = sUserAgent.match(/ipad/i) == 'ipad';var bIsIphone = sUserAgent.match(/iphone os/i) == 'iphone os';var bIsMidp = sUserAgent.match(/midp/i) == 'midp';var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == 'rv:1.2.3.4';var bIsUc = sUserAgent.match(/ucweb/i) == 'web';var bIsCE = sUserAgent.match(/windows ce/i) == 'windows ce';var bIsWM = sUserAgent.match(/windows mobile/i) == 'windows mobile';if(bIsIpad || bIsIphone || bIsMidp || bIsUc7 || bIsUc || bIsCE || bIsWM){window.location.href = 'http://www.163.com/';}else{window.location.href = 'http://www.baidu.com/';}})();</script></head><body><h1>pc瀏覽</h1></body></html>