【发布时间】:2015-09-29 19:58:31
【问题描述】:
我尝试在延迟 3 秒后使用 javascript 进行重定向,但我的代码目前什么都不做。 index.html 应该在 3 秒延迟后重定向到 menupage.html,但没有任何反应。 menupage.html 上有 4 个按钮:“设备运动”使用 Phonegap 的加速度计和指南针来显示设备的速度和方向,其他 3 个只是在 Phonegap 的应用内浏览器中打开新页面。代码如下:
编辑:这只是一个文件位置错误。重定向有效。谢谢!
<!--index.html-->
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0" />
<head>
<meta charset="utf-8" />
<title></title>
<link href="JQueryMobile/jquery.mobile-1.2.0.css" rel="stylesheet" />
<script src="JQueryMobile/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="JQueryMobile/jqm-docs.js" type="text/javascript"></script>
<script src="JQueryMobile/jquery.mobile-1.2.0.js" type="text/javascript"></script>
<link rel="stylesheet" href="styles/styles.css" />
</head>
<body onload="redirect()">
<img id="load-img" src="loading.jpg" />
<script src="scripts/scripts.js"></script>
</body>
</html>
<!--menupage.html-->
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<meta charset="utf-8" />
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0" />
<head>
<title>Menu</title>
<script type="text/javascript" src="phonegap.js"></script>
<link href="styles/styles.css" rel="stylesheet" />
</head>
<body>
<button id="devmotion" onclick="getMotion()">Device Motion</button>
<button id="ucla" onclick="openUCLA()">UCLA</button>
<button id="espn" onclick="openESPN()">ESPN</button>
<button id="google" onclick="openGoogle()">Google</button>
<script src="scripts/scripts.js"></script>
<script src="scripts/jquery-2.1.1.min.js"></script>
</body>
</html>
<!--devicemotion.html-->
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="viewport" content="user-scalable=yes, initial-scale=1.0, maximum-scale=1.0" />
<head>
<title>Acceleration and Compass Direction</title>
<script type="text/javascript" src="phonegap.js"></script>
<link href="styles/styles.css" rel="stylesheet" />
</head>
<body>
<div id="accel"></div>
<div id="compassdirection"></div>
<button id="goback" onclick="backToMenu()">Back</button>
<script src="scripts/scripts.js"></script>
<script src="scripts/jquery-2.1.1.min.js"></script>
</body>
</html>
<!--scripts.js-->
// JavaScript source code
function redirect() {
window.setTimeout(function() {
window.location.replace("menupage.html");
}, 3000)
}
function getMotion() {
window.location = "devicemotion.html";
//window.open("devicemotion.html", "_self", "location=yes");
navigator.accelerometer.getCurrentAcceleration(accelerometerSuccess, accelerometerError);
navigator.compass.getCurrentHeading(compassSuccess, compassError);
}
function backToMenu() {
window.location = "menupage.html";
}
function accelerometerSuccess(acceleration) {
acclrtn = 'Acceleration X: ' + acceleration.x + '\n' +
'Acceleration Y: ' + acceleration.y + '\n' +
'Acceleration Z: ' + acceleration.z + '\n' +
'Timestamp: ' + acceleration.timestamp + '\n';
document.getElementById("accel").innerHTML = acclrtn;
};
function accelerometerError(error) {
alert("Accelerometer error: " + accelerometer.code);
}
function compassSuccess(heading) {
direction = "Direction: " + heading.magneticHeading;
document.getElementById("compassdirection").innerHTML = direction;
}
function compassError(error) {
alert("Compass error: "+error.code);
}
function openUCLA() {
window.open('www.ucla.edu', '_blank', 'location=yes');
}
function openESPN() {
window.open('www.espn.com', '_blank', 'location=yes');
}
function openGoogle() {
window.open('www.google.com', '_blank', 'location=yes');
}
【问题讨论】:
-
window.location.replace并没有做你认为的那样...... -
似乎没有什么触发
redirect()。是故意的吗? -
这行不是在 index.html 中触发的吗?
标签: javascript html http-redirect