如何替换Cordova中的主页?
How to Replace the Main Page in Cordova?
我制作了登录功能,所以当用户登录时,它会导航到其他页面,但问题是登录成功后,用户只需按物理后退按钮即可再次返回登录页面。现在我想要的是登录后用户无法再次返回登录页面,因此当按下物理后退按钮时它会关闭应用程序。这是我的 onloginsucces 方法
document.getElementById('auth').onclick = function () {
$fh.auth({
"policyId": "policyid",
"clientToken": "appid",
// Your App GUID
"endRedirectUrl": window.location.href,
"params": {
"userId": document.getElementById('username').value,
"password": document.getElementById('password').value
}
}, function(res) {
// Authentication successful - store sessionToken in variable
var sessionToken = res.sessionToken;
alert("Login Succes");
window.location = './menu.html';
}, function(msg, err) {
var errorMsg = err.message;
if (errorMsg === "user_purge_data" || errorMsg === "device_purge_data") {
// User or device has been black listed from administration console and all local data should be wiped
} else {
alert("Authentication failed - " + errorMsg);
}
})
};
除了使用 window.location 导航之外还有其他方法吗?可能喜欢 window.replace,谢谢?
更新
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>Hello World</title>
<link rel="stylesheet" href="css/app.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="cordova-2.7.0.js"></script>
<script type="text/javascript" src="js/jquery.min.1.9.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.3.1.min.js"></script>
<script type="text/javascript">
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// device APIs are available
//
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
}
// Handle the back button
//
function onBackKeyDown() {
e.preventDefault();
}
</script>
</head>
尝试以下:
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e) {
#or return False
e.preventDefault();
}
该函数禁用按钮功能。我从不久前在 Whosebug page 上找到的一个页面中提取了这个。我认为 return False 可以正常工作,但可能不是 JS 的正确用法。
感谢评论者的编辑:请注意,这需要在设备上的 Cordova 中准备好侦听器才能工作。
我制作了登录功能,所以当用户登录时,它会导航到其他页面,但问题是登录成功后,用户只需按物理后退按钮即可再次返回登录页面。现在我想要的是登录后用户无法再次返回登录页面,因此当按下物理后退按钮时它会关闭应用程序。这是我的 onloginsucces 方法
document.getElementById('auth').onclick = function () {
$fh.auth({
"policyId": "policyid",
"clientToken": "appid",
// Your App GUID
"endRedirectUrl": window.location.href,
"params": {
"userId": document.getElementById('username').value,
"password": document.getElementById('password').value
}
}, function(res) {
// Authentication successful - store sessionToken in variable
var sessionToken = res.sessionToken;
alert("Login Succes");
window.location = './menu.html';
}, function(msg, err) {
var errorMsg = err.message;
if (errorMsg === "user_purge_data" || errorMsg === "device_purge_data") {
// User or device has been black listed from administration console and all local data should be wiped
} else {
alert("Authentication failed - " + errorMsg);
}
})
};
除了使用 window.location 导航之外还有其他方法吗?可能喜欢 window.replace,谢谢?
更新
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>Hello World</title>
<link rel="stylesheet" href="css/app.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="cordova-2.7.0.js"></script>
<script type="text/javascript" src="js/jquery.min.1.9.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.3.1.min.js"></script>
<script type="text/javascript">
function onLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
// device APIs are available
//
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
}
// Handle the back button
//
function onBackKeyDown() {
e.preventDefault();
}
</script>
</head>
尝试以下:
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e) {
#or return False
e.preventDefault();
}
该函数禁用按钮功能。我从不久前在 Whosebug page 上找到的一个页面中提取了这个。我认为 return False 可以正常工作,但可能不是 JS 的正确用法。
感谢评论者的编辑:请注意,这需要在设备上的 Cordova 中准备好侦听器才能工作。