[英]JavaScript not detecting mobile device and redirecting
我希望能够确定设备是android还是iphone,然后进行相应的重定向。 但是,当我测试它不起作用时,它只会加载html文件。 任何想法为什么这不起作用? 我也真的没有Java脚本的经验。 我主要坚持移动开发。 我在iPhone和Android上打开了网址,但都没有运气。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script>
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
if (isAndroid) {
// Do something!
// Redirect to Android-site?
window.location = 'http://espn.go.com/';
}
if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
if (document.cookie.indexOf("iphone_redirect=false") == - 1) window.location = "http://www.yahoo.com/;
}
</script>
<title>Redirect</title>
<script>
// google analytic code is here
</script>
Redirecting
</body>
</html>
设置document.location
而不是window.location
。
document.location = "http://google.com"
更新
实际上,您仍然可以使用window.location
,只是在网址中遗漏了结尾的"
:
错误: window.location = "http://google.com;
良好: window.location = "http://google.com";
您只有语法错误:
window.location = "http://www.yahoo.com/;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.