views:

531

answers:

4

Here is the situation :

If i am in page-1 now i am clicking a link from page-1 to navigate to page-2. Before page-2 is loaded i am hitting escape so that still i am staying in page-1.

At that point i want to track that event.

Is there any javascript event so that i can track the above scenario.

More Info :

To avoid concurrent request to the "page-2", when the user clicks a link from page-1 i am redirecting to page-2 and disabling the link (to avoid multiple request to "page-2). At this point when we hit Esc and abort loading page-2, i need to enable the link again in page-1.

+3  A: 

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://stackoverflow.com"&gt;Stack Overflow</a>
</body>
</html>

It detects any key pressed while you're on the page. Once you click on the SO link and hit escape, nothing happens. The browser is already receiving a response from the SO server and is starting to process it, this page has already been "left", despite appearances when you see "Waiting for http://stackoverflow.com" in your browser's status bar.

Mr. Smith
A: 

Two events are triggered when the page unloads.

window.onUnload
window.onBeforeUnload

You can use these events to cancel the unload of the page, however after that, there page is considered done.

What you can do is make the page wait 5 secs or so before going to the new page:

eg:

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

In fact that may cause some browsers to hang since you're taking up all process time given to the JS script. ie: in the while block.

You could probably avoid that by doing a blocking function call, that isn't so process intensive, like one that is bound by network latency. eg: XMLHttpRequest() etc. I don't think calls that are queued such as setTimeout() will work here, as they don't block.

bucabay
Yes that will freeze the browser. Ugh.
Crescent Fresh
A: 

Since the <a href> tag tells the browser to move to another page, it's up to it to decide if it will still have your script running. If I were it, I wouldn't listen.

If you want to override that, I guess you should tell the browser not to listen to that particular onClick event, and put a callback in place that loads the target page in the background. This assures your page is still active. After the target page has loaded (by your script), you could kindly ask the browser to update itself with the received content.

So that's the theory. I have no idea if the DOM lets you just override the content of a loaded page, but I guess it does.

xtofl
If the target page is on a different server, JS won't let you request it.
Alsciende
I'm not the expert - there's no possible HttpRequest class for javascript? You can do XMLHttpRequest, can't you?
xtofl
+1  A: 

Your idea of handling this event is plain wrong. Blocking the button is required to make the user unable to do double post data. However, the request is sent instantaneously(!) after the click on the link.

So, if you click the link once, stop the page, then click second time - it will submit it twice, and that is not what is intended to happen.

Max