Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 重新加载前的窗口确认消息_Javascript_Angularjs - Fatal编程技术网

Javascript 重新加载前的窗口确认消息

Javascript 重新加载前的窗口确认消息,javascript,angularjs,Javascript,Angularjs,我的代码运行良好,但我只是想知道这是否正常。。我在Linux中尝试了我的代码,在退出页面或重新加载之前,我的确认消息可以更改。然而,当我在Mac电脑上试用时,我看到的唯一信息是“您所做的更改可能无法保存”。是因为代码还是因为操作系统,消息的行为是这样的。它发生在Firefox的Chrome浏览器中 代码是: <!DOCTYPE html> <html data-ng-app="TestApp"> <head> <script src="http://co

我的代码运行良好,但我只是想知道这是否正常。。我在Linux中尝试了我的代码,在退出页面或重新加载之前,我的确认消息可以更改。然而,当我在Mac电脑上试用时,我看到的唯一信息是“您所做的更改可能无法保存”。是因为代码还是因为操作系统,消息的行为是这样的。它发生在Firefox的Chrome浏览器中

代码是:

<!DOCTYPE html>
<html data-ng-app="TestApp">
<head>
<script src="http://code.angularjs.org/1.2.9/angular.js"></script>
<script>
angular.module('TestApp', [])
.factory('beforeUnload', function ($rootScope, $window) {
    // Events are broadcast outside the Scope Lifecycle

    $window.onbeforeunload = function (e) {
        var confirmation = {};
        var event = $rootScope.$broadcast('onBeforeUnload', confirmation);
        if (event.defaultPrevented) {
            return confirmation.message;
        }
    };

    $window.onunload = function () {
        $rootScope.$broadcast('onUnload');
    };
    return {};
})
.run(function (beforeUnload) {
    // Must invoke the service at least once
});
function TestController($scope) {
    $scope.$on('onBeforeUnload', function (e, confirmation) {
        confirmation.message = "All data willl be lost.";
        e.preventDefault();
    });
    $scope.$on('onUnload', function (e) {
        console.log('leaving page'); // Use 'Preserve Log' option in Console
    });
}
</script>
</head>
<body data-ng-controller="TestController">
This is a test
<a href="http://www.google.com/">Google</a>
</body>
</html>

角度模块('TestApp',[])
.factory('beforeUnload',函数($rootScope,$window){
//事件在作用域生命周期之外广播
$window.onbeforeunload=函数(e){
var确认={};
var事件=$rootScope.$broadcast('onBeforeUnload',确认);
if(event.defaultPrevented){
返回确认信息;
}
};
$window.onunload=函数(){
$rootScope.$broadcast('onUnload');
};
返回{};
})
.run(函数(卸载前){
//必须至少调用该服务一次
});
函数TestController($scope){
$scope.$on('onBeforeUnload',函数(e,确认){
confirmation.message=“所有数据都将丢失。”;
e、 预防默认值();
});
$scope.$on('onUnload',函数(e){
console.log('leaving page');//在控制台中使用'Preserve log'选项
});
}
这是一个测试
PS

上面的代码是AngularJS,但即使是使用纯javascript的相同功能也会以相同的方式运行。

代码很好

声明

在Firefox4和更高版本中,返回的字符串不会显示给用户。相反,Firefox显示字符串“此页面要求您确认是否要离开-您输入的数据可能无法保存。”


它还指出,Chrome 51.0和Firefox 44.0中的“自定义文本支持已删除”。

因此,答案如下。非常感谢你!我在重新加载系统时遇到另一个问题。“onUnload”没有任何作用。我试着提醒,但什么也没有happened@estus当用户关闭浏览器时,是否有办法向用户显示自定义消息?@R.S.K我想答案是否定的,至少我不知道。