how to detect if a link was clicked when [removed] is triggered?

前端 未结 3 1003
半阙折子戏
半阙折子戏 2020-12-02 00:13

I have window.onbeforeunload triggering properly. It\'s displaying a confirmation box to ensure the user knows they are navigating (closing) the window and that any unsaved

3条回答
  •  温柔的废话
    2020-12-02 01:02

    var link_was_clicked = false;
    document.addEventListener("click", function(e) {
      if (e.target.nodeName.toLowerCase() === 'a') {
        link_was_clicked = true;
      }
    }, true);
    
    window.onbeforeunload = function() {
      if(link_was_clicked) {
        link_was_clicked = false;
        return;
      }
      //other code here
    }
    

提交回复
热议问题