How to open a model or dialog box?

How to open a dialog when a user tries to refresh or reload a page?

I tried a lot of links that offered me to subtract keypress / keydown / beforeload / beforeunload updates, but they did not work somehow.

here, I tried. with my script.

var App = angular.module('App', ['ngRoute']);
        // configure our routes
        App.config(function($routeProvider) {
            $routeProvider
                .when('/', {
                    templateUrl : 'home.html',
                    controller  : 'mainController'
                })
                .when('/about', {
                    templateUrl : 'about.html',
                    controller  : 'aboutController'
                })
                .when('/contact', {
                    templateUrl : 'contact.html',
                    controller  : 'contactController'
                });
        });

 App.controller('mainController', function($scope) {
        //but not here if the user refresh the page 
        $scope.message = 'MainCtrl!';                    
    });

    App.controller('aboutController', function($scope) {
        $scope.message = 'aboutController';
       // if this is the current controller and page being refresh by the user activity
       // prompt the user to save before you reload...
    });

    App.controller('contactController', function($scope) {
        $scope.message = 'contactController';
    // if this is the current controller and page being refresh by the user activity
       // prompt the user to save before you reload...
    });

 in index.html 
 --------------------
 <script> 
     //it is working . it is invoking in each page. But needs to invoke at certain controller means it should check 
     // it is current/exact page where user has to save data before reloading 

     jQuery(function () {          
    // to check and get page refreshed & reload 

    var myEvent = window.attachEvent || window.addEventListener;
    var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload';  

    myEvent(chkevent, function (e) {  
        var confirmationMessage = ' ';   
        (e || window.event).returnValue = confirmationMessage;
        return confirmationMessage;
    });
    if (myEvent) {
        localStorage.clear();
        // redirect to 
        $state.go('gotohome');                                 
    }
});
</script>

Can someone give me some functional example that will work in all types of browsers?

This is so important to me, I can’t do it.

Thank.

+4
source share
4 answers

Confirmation with session variables is explained in the following article:   http://www.tedpavlic.com/post_detect_refresh_with_javascript.php

0
source

, ​​ :

, Javascript

, , , .

0

I have two solutions - this is my strategy, which is to open a dialog on a specific page.

$rootScope.refreshmodal= false;
    $rootScope.refreshModal = function(){
    $rootScope.refreshmodal = $rootScope.refreshmodal ? false : true;
}



window.onload = function (e) {                       
    if(e.returnValue){   
        if(e.currentTarget.location.pathname == "/contact") {
            //alert("ask to user ")
            $rootScope.refreshmodal=true; // opening a UI modal
        }else if(e.currentTarget.location.pathname == "/about"){
            $rootScope.refreshmodal=true; // opening a UI modal                                  
        }else {
                alert(" donot ask ");
             }  
   } 

This does not work on onforeforeload.

2nd from here: https://gist.github.com/981746/3b6050052ffafef0b4df and it is not redirected after confirmation to the house.

0
source

since you are coding in angular you can try this

$window.onbeforeunload = function (e) {  
                return "";
            }; 
            $window.onload=function(){              
                window.location.href="gowhereever you want";
            }
0
source

Source: https://habr.com/ru/post/1687041/


All Articles