3

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
  • I am not sure this is possible without some timer or browser hack. And if you use that hack, a lot of issues with different browsers will appear. Could you please explain why do you need that event? Maybe there is a better way of solving your problem? Commented Sep 2, 2009 at 6:19
  • 1
    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. Commented Sep 2, 2009 at 6:39
  • @Madhu: that extra info is helpful, you should add it to the question. Commented Sep 2, 2009 at 7:20

4 Answers 4

3

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">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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

1 Comment

Yes that will freeze the browser. Ugh.
0

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.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.