我已经实现了代码,以便禁用Android设备后退按钮,但它不起作用。当我运行我的应用程序并点击后退按钮时,它只是退出。我在我的index.html文件中包含了cordova.js。我究竟做错了什么? 这是我的代码:
JS
document.addEventListener('deviceready', function () {
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e) {
e.preventDefault();
}
}, false);
的index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<!--<link href="assets/ratchet/css/ratchet.css" rel="stylesheet">
<link href="assets/css/styles.css" rel="stylesheet"> -->
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<link href="css/style.css" rel="stylesheet">
<!-- <link href="assets/jqueryui/jquery-ui.css" rel="stylesheet"> -->
</head>
<body>
<div class="maincontainer">
<script id="langue-tpl" type="text/template">
<div id="mydialog" title="Beezlink"></div>
<div class="sk-circle" id="proc"><div class="sk-circle1 sk-child"></div><div class="sk-circle2 sk-child"></div><div class="sk-circle3 sk-child"></div><div class="sk-circle4 sk-child"></div><div class="sk-circle5 sk-child"></div><div class="sk-circle6 sk-child"></div><div class="sk-circle7 sk-child"></div><div class="sk-circle8 sk-child"></div><div class="sk-circle9 sk-child"></div><div class="sk-circle10 sk-child"></div><div class="sk-circle11 sk-child"></div><div class="sk-circle12 sk-child"></div></div>
<div class="contentwrap home">
<div id="bodydiv">
<!--
<img src="assets/logo-symbol.png" class="home-logo" />
<div>
<div class="form-group">
<select id="langtr" class="form-control">
<option value="val_fr">Francais</option>
<option value="val_english">English</option>
<option value="val_spanish">Spanish</option>
<option value="val_german">German</option>
</select>
<br/><button id="gobtn">OK</button>
</div>
</div>
-->
</div>
</div>
</script>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script src="lib/handlebars"></script>
<script type='application/javascript' src='lib/fastclick'></script>
<script src="lib/jquery"></script>
<script src="lib/jquery-ui"></script>
<script src="lib/jquery.alphanum"></script>
<script src="js/LangueView"></script>
<script src="js/app"></script>
<script src="js/disablerclick"></script>
</body>
</html>
app.js
// We use an "Immediate Function" to initialize the application to avoid leaving anything behind in the global scope
(function () {
Handlebars.registerHelper('compare', function (lvalue, operator, rvalue, options) {
var operators, result;
if (arguments.length < 3) {
//throw new Error("Handlerbars Helper 'compare' a besoin de 2 parametres");
}
if (options === undefined) {
options = rvalue;
rvalue = operator;
operator = "===";
}
operators = {
'==': function (l, r) { return l == r; },
'===': function (l, r) { return l === r; },
'!=': function (l, r) { return l != r; },
'!==': function (l, r) { return l !== r; },
'<': function (l, r) { return l < r; },
'>': function (l, r) { return l > r; },
'<=': function (l, r) { return l <= r; },
'>=': function (l, r) { return l >= r; },
'typeof': function (l, r) { return typeof l == r; }
};
if (!operators[operator]) {
//throw new Error("Handlerbars Helper 'compare' ne connait pas l'operateur " + operator);
}
result = operators[operator](lvalue, rvalue);
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
LangueView.prototype.template = Handlebars.compile($("#langue-tpl").html());
$('body').html(new LangueView().render().$el);
/* ---------------------------------- Local Variables ---------------------------------- */
// var pictureSource; // picture source
//var destinationType; // sets the format of returned value
// Wait for device API libraries to load
//
/*var service = new EmployeeService();
service.initialize().done(function () {
$('body').html(new HomeView(service).render().$el);
});*/
/* --------------------------------- Event Registration -------------------------------- */
//$('.search-key').on('keyup', findByName);
/*$('.help-btn').on('click', function() {
alertjqry("Employee Directory v3.4");
});*/
/*
window.addEventListener('orientationchange', function () { //window.orientation=='-90' || window.orientation=='90'
if(window.innerHeight < window.innerWidth){
if(window.orientation=='-90' || window.orientation=='90')
alertjqry("Attention: Si vous avez un iphone 6, veuillez retourner en mode portrait sinon certaines fonctions de l'application pourront mal fonctionner avec cette orientation de votre appareil!");
}else{
$("#mydialog").dialog('close');
}
}, false);
*/
document.addEventListener('deviceready', function () {
FastClick.attach(document.body);
//console.log(navigator.contacts);
// console.log(navigator.camera);
//console.log(FileTransfer);
//window.open= cordova.InAppBrowser.open;
//pictureSource = navigator.camera.PictureSourceType;
//destinationType = navigator.camera.DestinationType;
/*
if (navigator.notification) { // Override default HTML alert with native dialog
window.alert = function (message) {
navigator.notification.alert(
message, // message
null, // callback
"Beezlink", // title
'OK' // buttonName
);
};
}
*/
document.addEventListener("backbutton", onBackKeyDown, false);
}, false);
function onBackKeyDown(e) {
e.preventDefault();
}
//console.log(FileTransfer);
/* ---------------------------------- Local Functions ---------------------------------- */
}());
答案 0 :(得分:0)
尝试添加 e.stopPropagation()或 e.stopImmediatePropagation() 而不是e.preventDefault();
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
document.addEventListener("backbutton", function (e) {
e.preventDefault(); OR e.stopPropagation();
}, false );
}
常见错误是在桌面和cordova上执行开发 脚本被排除。然后忘记包括cordova脚本 移动版。
答案 1 :(得分:0)
您可能需要阻止像popstate这样的其他窗口事件。 试试这个:
window.addEventListener("popstate", function(e) {
//try e.preventDefault() or e.stopImmediatePropagation()
e.stopImmediatePropagation()
e.preventDefault();
}, false);
答案 2 :(得分:0)
我跳过onDeviceReady,所以我只有:
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e) {
e.preventDefault();
}
这对我有用