Javascript Angularjs Onbeforeunload在没有提示的情况下工作不正常
我想在窗口关闭前执行一些代码,但不想显示提示。 这是我的密码:Javascript Angularjs Onbeforeunload在没有提示的情况下工作不正常,javascript,angularjs,onbeforeunload,Javascript,Angularjs,Onbeforeunload,我想在窗口关闭前执行一些代码,但不想显示提示。 这是我的密码: $window.onbeforeunload = function(){ $rootScope.$broadcast('war-change'); return undefined; }; $scope.$on('war-change',function(){ console.log('here'); //Execute some more co
$window.onbeforeunload = function(){
$rootScope.$broadcast('war-change');
return undefined;
};
$scope.$on('war-change',function(){
console.log('here');
//Execute some more code here.
});
返回undefined或false不起作用,因为我的代码没有得到执行。
非常感谢您的帮助。您的代码确实按预期工作。检查以下小提琴: 但是,使用beforeunload方法可以做的事情有一些限制,因为一旦提交了unload,您就有非常有限的时间来做一些事情。要测试您有多少时间,可以使用以下修改:
$scope.$on('war-change',function(){
console.log('here');
//Execute some more code here.
setTimeout(function(){ console.log("ha-ha")}, 100)
console.log("he-he")
});
现在,如果您将超时减少到0-100,您仍然可以在输出中看到“ha”。增加到2秒(2000),你很可能看不到那一行 您的代码确实按预期工作。检查以下小提琴: 但是,使用beforeunload方法可以做的事情有一些限制,因为一旦提交了unload,您就有非常有限的时间来做一些事情。要测试您有多少时间,可以使用以下修改:
$scope.$on('war-change',function(){
console.log('here');
//Execute some more code here.
setTimeout(function(){ console.log("ha-ha")}, 100)
console.log("he-he")
});
现在,如果您将超时减少到0-100,您仍然可以在输出中看到“ha”。增加到2秒(2000),你很可能看不到那一行
$scope.$on('war-change',function(){
console.log('here');
//Execute some more code here.
setTimeout(function(){ console.log("ha-ha")}, 100)
console.log("he-he")
});