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