我正在尝试重定向我的页面,如果它是在iPhone 5上查看
这是更新后的当前代码:
<html>
<head>
<title>TEST</title>
</head>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.js">
$(document).ready(function () {
var isiPhone = navigator.userAgent.toLowerCase().indexOf("iphone");
var iHeight = window.screen.height;
if (isiPhone > -1 && iHeight <= 1336) {
window.location("index2.html");
}
});
</script>
Start Page
</body>
</html>
谢谢。
刚刚注意到一个巨大的错误。要么你的<script>
标签包含代码或它有一个src
属性,但它不能有两个…
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.js">
$(document).ready(function () {
// your code
});
// your code cannot be inside a JavaScript file include
// this needs to be totally empty!
</script>
相反,你必须这样做。
注意JavaScript文件的空<script></script>
标记集包括..
<script type="text/javascript" src="yourfile.js"></script>
<script type="text/javascript">
// your code in here
</script>
您有多余的圆括号。
if(isiPhone > -1) && (iHeight <= 1336) {
应该是:
if ( isiPhone > -1 && iHeight <= 1336 ) {
编辑:试着调试代码,看看为什么它不能工作:
var isiPhone = navigator.userAgent.toLowerCase().indexOf("iphone");
var iHeight = window.screen.height;
if (isiPhone > -1 && iHeight <= 1336) {
window.location.replace("index2.html")
} else {
console.log('Is this browser an iPhone: ' + ((isiPhone > -1) ? 'yes' : 'no'));
console.log('Is the height <= 1336: ' + ((iHeight <= 1336) ? 'yes' : 'no'));
}