Javascript interrupt event

Here is the situation:

If I am now on page 1, I click the link from page-1 to go to page-2. Before loading page-2, I am on the run, so I still remain on page-1.

At this moment I want to track this event.

Is there any javascript event so that I can track the above script.

Additional Information:

To avoid a parallel request to "page-2" when the user clicks a link from page-1, I redirect to page-2 and turn off the link (to avoid multiple requests to page-2). when we press Esc and abort page-2 loading, I need to enable the link on page-1 again.

+3
source share
4 answers

I tried using this code:

<html>
<head>
    <script>
    document.onkeypress = KeyPressed;

    function KeyPressed(e)
    {
        if (!e) e = window.event; //IE Compatibility
        alert(e.keyCode);
    }
    </script>
</head>

<body>
    <a href="http://qaru.site/">Stack Overflow</a>
</body>
</html>

It detects any key pressed while on the page. As soon as you click on the SO link and hit escape, nothing will happen. The browser already receives a response from the SO server and starts to process it, this page has already been “abandoned”, despite the appearance when you see “Waiting for /fooobar.com / ... ” in the status bar of your browser.

+3
source

Your idea of ​​dealing with this event is simple. A button lock is required so that the user cannot execute double messages. However, the request is sent instantly (!) After clicking on the link.

, , , - ​​ , , .

+1

.

window.onUnload
window.onBeforeUnload

, , .

, 5 , :

:

window.onunload = (function() {
  var time = new Date();
  var cancel = false;
  window.onkeypress = function (e) {
    if ((e || window.event).keyCode == 27) cancel = true;
  };
  while(time > new Date() - 5000) {
    if (cancel) return false;
  }
});

, , JS script. ..: while.

, , , , , . : XMLHttpRequest() .. , , , setTimeout(), , .

0

<a href> , , script. , .

, , onClick , . , . ( script) .

, . , DOM , , .

0

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


All Articles