写了一个简单的函数:利用navigator对象的userAgent属性判断用户浏览器是PC端还是移动端;不知道有哪些缺陷,各位大神帮忙分析下,谢谢! !DOCTYPE htmlhtml lang="en"head meta charset="UTF-8" titl
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>mobile-pc</title> <script type="text/javascript"> function getuser(){ var scan = navigator.userAgent; document.write(scan); if(scan.indexOf("Mobile") > -1) return "mobile"; else if(scan.indexOf("Andriod") > -1 || scan.indexOf("Linux") > -1) return "andriod"; else if(scan.indexOf("iPhone") > -1 || scan.indexOf("Mac") > -1) return "iphone"; else if(scan.indexOf("iPad") > -1) return "ipad"; else return "pc"; } </script> </head> <body> <script type="text/javascript"> var bres = getuser(); if(bres == "mobile" || bres == "andriod" || bres == "iphone" || bres == "ipad") document.write("<br />"+"mobile"); //window.location = "/mobile/home.html"; else if(bres == "pc") document.write("<br />"+"pc"); //window.location = "/pc/home.html"; </script> </body> </html>