3

Possible Duplicate:
How can i get the destination url in javascript onbeforeunload event?

Is there a way to identify the pending navigation uri from within an onunload event? I'm wanting to take specific actions depending on where the user navigates on the page (particular links, back navigation, refresh, etc.) Is there a better way to approach the task?

In an effort to be all-inclusive, I'm avoiding adding listeners to links and similarly don't want to use popstate, hashchange, etc. onunload seemed to be the simplest way to catch almost any scenario, but I haven't been able to find a way to see where new page will be loaded to within this scope. Can it be done?


As mentioned in the comments, I don't completely agree that this is a duplicate of the post above. I definitely don't think it should have been closed because now no one can provide any kind of answer, including myself. In any case, here is the solution I used:

$(document).ready(function() {
    var newUri;
    $(document).on("click", "*", function(e) {
        var thisElement = $(this)[0];
        if (thisElement.onclick) {
            // Handle special case situations.
        }
        if (thisElement.href) {
            newUri = thisElement.href;
            console.log(thisElement);
        }
    });
    window.onbeforeunload = function(e) {
        console.log("Client navigating to "+newUri);
        // Perform final handling steps.
    };
});
Community
  • 1
  • 1
jtrick
  • 1,329
  • 18
  • 23
  • Thanks for the link Brad; not sure how I didn't pull that one along with the 20 or so that I scoured through before posting. It is a pretty similar post, although I'm not interested in navigation away from the page when the user types it explicitly. Regardless, from what I've found so far it seems that this (intentionally) may not be possible... – jtrick Nov 24 '12 at 07:04
  • Yep, regardless of whether or not you are going to another page, I think the same problem is there. If you were a jQuery user, I would recommend putting a delegate click handler on the body, or something similar to catch events as they bubble up. You can do the same thing without jQuery, but I cannot recommend a good method off the top of my head, as I haven't worked with the raw DOM objects in awhile. – Brad Nov 24 '12 at 07:06
  • Ok thanks again, I'll just go that route. – jtrick Nov 24 '12 at 07:09
  • I would actually say that this question is different enough from the one it is marked as a duplicate of because of the emphasis there of obtaining "what the user types into the url box of the browser." Unfortunately I don't see a way of debating that however and now I can't answer my own question here... For anyone interested or dealing with a situation like mine, I'll post the solution I used in the following comment: – jtrick Nov 24 '12 at 21:44
  • Because of the way that code is formatted in comments, I deleted it and just edited the question itself. Maybe it will help someone. – jtrick Nov 24 '12 at 22:04
  • 2
    I've voted to re-open your question. – Brad Nov 25 '12 at 01:00

0 Answers0