【问题标题】:How to open model or dialog box?如何打开模式或对话框?
【发布时间】:2018-03-18 12:21:23
【问题描述】:

当用户尝试刷新或重新加载页面时如何实现对话框打开?

我尝试了许多链接,这些链接建议我扣除刷新的按键/keydown/beforeload/beforeunload,但这些都不起作用。

这里是,我试过了。与我的情况。

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>

谁能给我任何适用于所有类型浏览器的功能示例?

对我来说太重要了,反正我做不到。

谢谢。

【问题讨论】:

  • 您必须说出您尝试过的代码,我们可以帮助您编写代码。
  • @artgb,我已经编辑并给出了我的场景。

标签: javascript jquery angularjs


【解决方案1】:

下面的文章解释了带有会话变量的方法: http://www.tedpavlic.com/post_detect_refresh_with_javascript.php

【讨论】:

    【解决方案2】:

    我建议您以此作为参考来检测页面是否已刷新或重新加载:

    Check if page gets reloaded or refreshed in Javascript

    请提供一些代码,以便我们帮助您打开对话框。

    【讨论】:

      【解决方案3】:

      我有两个解决方案,一个是我的策略,即在特定页面上打开对话框。

      $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 ");
                   }  
         } 
      

      这不适用于 onbeforeload。

      第二个来自这里:https://gist.github.com/981746/3b6050052ffafef0b4df 并且在确认到家后不会重定向到。

      【讨论】:

        【解决方案4】:

        当你用 Angular 编码时,你可以试试这个

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-04-06
          • 1970-01-01
          • 1970-01-01
          • 2020-06-26
          • 1970-01-01
          • 1970-01-01
          • 2013-06-09
          相关资源
          最近更新 更多