복붙노트

[JQUERY] 페이지를 (떠나지 않을) 닫을 때 JQuery와는 beforeunload?

JQUERY

페이지를 (떠나지 않을) 닫을 때 JQuery와는 beforeunload?

해결법


  1. 1.당신은 jQuery를 사용하여이 작업을 수행 할 수 있습니다.

    당신은 jQuery를 사용하여이 작업을 수행 할 수 있습니다.

    예를 들어,

    <a href="your URL" id="navigate"> click here </a>
    

    귀하의 JQuery와는 것

    $(document).ready(function(){
    
        $('a').on('mousedown', stopNavigate);
    
        $('a').on('mouseleave', function () {
               $(window).on('beforeunload', function(){
                      return 'Are you sure you want to leave?';
               });
        });
    });
    
    function stopNavigate(){    
        $(window).off('beforeunload');
    }
    

    그리고 휴가 메시지 경고가 될 것입니다 얻을,

    $(window).on('beforeunload', function(){
          return 'Are you sure you want to leave?';
    });
    
    $(window).on('unload', function(){
    
             logout();
    
    });
    

    이 솔루션은 모든 브라우저에서 작동하고 나는 그것을 테스트했습니다.


  2. 2.당신의 아약스에 자바 스크립트 시도

    당신의 아약스에 자바 스크립트 시도

    window.onbeforeunload = function(){
      return 'Are you sure you want to leave?';
    };
    

    참조 링크

    예 2 :

    document.getElementsByClassName('eStore_buy_now_button')[0].onclick = function(){
        window.btn_clicked = true;
    };
    window.onbeforeunload = function(){
        if(!window.btn_clicked){
            return 'You must click "Buy Now" to make payment and finish your order. If you leave now your order will be canceled.';
        }
    };
    

    그는 버튼을 클릭 할 때까지 여기, 사용자에게 그 페이지를 잎마다 알려줍니다.

    DEMO : http://jsfiddle.net/DerekL/GSWbB/show/


  3. 3.신용 여기에 가야한다 : window.onbeforeunload가 트리거 될 때 링크가 클릭 된 경우 어떻게 감지?

    신용 여기에 가야한다 : window.onbeforeunload가 트리거 될 때 링크가 클릭 된 경우 어떻게 감지?

    기본적으로,이 솔루션은 링크 나 윈도우가 불에 unload 이벤트가 발생하면 감지하는 리스너를 추가합니다.

    var link_was_clicked = false;
    document.addEventListener("click", function(e) {
       if (e.target.nodeName.toLowerCase() === 'a') {
          link_was_clicked = true;
       }
    }, true);
    
    window.onbeforeunload = function(e) {
        if(link_was_clicked) {
            return;
        }
        return confirm('Are you sure?');
    }
    

  4. 4.여기 https://stackoverflow.com/a/1632004/330867를 바와 같이, 당신은 무엇을이 페이지의 출구를 기원한다 "필터링"하여 구현할 수 있습니다.

    여기 https://stackoverflow.com/a/1632004/330867를 바와 같이, 당신은 무엇을이 페이지의 출구를 기원한다 "필터링"하여 구현할 수 있습니다.

    코멘트에서 언급 한 바와 같이, 여기 또한 당신의 질문에 만드는 아약스 요청을 포함 다른 질문의 코드의 새로운 버전입니다 :

    var canExit = true;
    
    // For every function that will call an ajax query, you need to set the var "canExit" to false, then set it to false once the ajax is finished.
    
    function checkCart() {
      canExit = false;
      $.ajax({
        url : 'index.php?route=module/cart/check',
        type : 'POST',
        dataType : 'json',
        success : function (result) {
           if (result) {
            canExit = true;
           }
        }
      })
    }
    
    $(document).on('click', 'a', function() {canExit = true;}); // can exit if it's a link
    $(window).on('beforeunload', function() {
        if (canExit) return null; // null will allow exit without a question
        // Else, just return the message you want to display
        return "Do you really want to close?";
    });
    

    중요 : (여기 canExit) 정의 전역 변수가 없어야합니다,이 간단한 버전 여기에있다.

    당신이 (적어도 크롬에서) 완전히 확인 메시지를 무시할 수 없습니다. 당신이 돌아가 메시지는 크롬에 의해 주어진 하나의 앞에 추가됩니다. 어떻게이입니다 onbeforeunload 대화 상자를 무시하고 내 자신의로 교체 할 수 있습니다 : 여기에 이유가?


  5. 5.아약스를 통해이 데이터로드를 시도하고 return 문을 통해 표시.

    아약스를 통해이 데이터로드를 시도하고 return 문을 통해 표시.

    <script type="text/javascript">
    function closeWindow(){
    
        var Data = $.ajax({
            type : "POST",
            url : "file.txt",  //loading a simple text file for sample.
            cache : false,
            global : false,
            async : false,
            success : function(data) {
                return data;
            }
    
        }).responseText;
    
    
        return "Are you sure you want to leave the page? You still have "+Data+" items in your shopping cart";
    }
    
    window.onbeforeunload = closeWindow;
    </script>
    

  6. 6.당신은 'onbeforeunload'이벤트를 시도 할 수 있습니다.

    당신은 'onbeforeunload'이벤트를 시도 할 수 있습니다.

    또한 this-에서보세요

    대화 상자 실행 1 초 사라질 하시나요?

  7. from https://stackoverflow.com/questions/18783535/jquery-beforeunload-when-closing-not-leaving-the-page by cc-by-sa and MIT license