簡體   English   中英

在用戶離開頁面時提醒用戶

[英]Alert user when they are leaving a page

我想提醒用戶何時離開頁面,無論是通過關閉選項卡或窗口,還是通過鏈接進入新的URL等方式。目前,我有以下代碼:

$scope.$on('$locationChangeStart', function (event, next, current) {
    if (current.match("fairs/")){
        var answer = confirm("Are you sure you want to leave this page?");
        if (!answer) {
            event.preventDefault();
        }
    }
}

這是什么,當我離開帶有'fairs /'(URI)的頁面時,它會彈出確認框。 但是,它在頁面重定向彈出,因此,如果單擊“取消”並嘗試阻止默認事件,它將保留在重定向的頁面上。 然后,如果您嘗試離開該頁面,則確認框將再次彈出,因為瀏覽器仍然認為它位於“展覽/”頁面上。

我從另一個問題中得到了解決方案,但這對我沒有用。 我如何獲得它,以便在重定向頁面之前彈出確認框?

 <script type="text/javascript"> var popit = true; window.onbeforeunload = function() { if(popit == true) { popit = false; return "Are you sure you want to leave?"; } } </script> 

試試上面

您可以在角路由中使用'resolve'運行函數( docs router

我認為類似的東西應該起作用:

$routeProvider.when('/myPage', { templateUrl: 'myPage.html',
        resolve: {
            myFunction: function ($route) { 
               if (myCondition) {
                   $location.url('/myPage');
                   return;
               } 
            }
        }
    });

您還可以在事件$routeChangeStart上放置一個偵聽$routeChangeStart ,並根據情況進行中斷。 請在此處查看解決方案它可能會對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM