How can I get the destination URL for the onbeforeunload event?
Asked Answered
P

3

72

I've searched for hours, but I couldn't find a solution for this.

window.onbeforeunload = warn;

This doesn't work:

function warn (e) 
{ 
   var destination = e.href;
   alert(destination );
}

Okay, so to clear the things. If the user clicks on a link on the page itself, it is easy, because you can add an eventhandler to all of the links onclick event, but. I want to catch the address, what the user types into the url box of the browser.

Physiography answered 6/11, 2009 at 10:21 Comment(1)
I work on data processing sites and before a user leaves the page I make sure all the data meets requirements. The onbeforeunload event is useful, but cumbersome because of the way browsers handle it. I can't stop the user from leaving the page, but it would be nice to know where they are going. As it happens, in my case, I do know because it is a site generated redirect, so I can set a session variable.Knuth
C
75

Because it can't be done. The new location is private/sensitive information. Nobody wants you to know which sites they visit when they leave your site.

Cage answered 6/11, 2009 at 10:32 Comment(8)
I think you got the question wrong, why would you use onbeforeonload if you go to a page on your own site? That makes no sense. Misnyo, can you clarify what you want exactly?Cage
@Marsha 's answer below, is interesting: https://mcmap.net/q/273786/-how-can-i-get-the-destination-url-for-the-onbeforeunload-eventPropylaeum
@MichelvanEngelen - b/c maybe you want to trigger a CSS loading screen to let the user know he's getting ready to navigate somewhere, that they did indeed click a link and something is happening (makes the site feel more responsive, especially when it takes a while to navigate away to another page)Headstrong
@jbyrd, possibly! However, I feel loaders belong to the destination page, not the page that we navigate from. Puts you more in control, and nowadays we often just present shells and load data via AJAX requests and the likes.Cage
@MichelvanEngelen - it's an interesting consideration. In fact, someone else on my team was the one to initially suggest this behavior, and not I. I was a little wary when he first suggested it, especially since I hadn't really seen it before. But the rest of the team liked it and I couldn't think of a major issue with it, so we went with it.Headstrong
@MichelvanEngelen - or maybe you want to trigger a request to drop user sessions.Tinatinamou
You can get Destination URL by using this document.activeElement.hrefCostermonger
For posterity - there is 1 valid reason to warn users to navigating to another page on your own site which is to prevent the loss of unsaved data, which is what this event is used for in a lot of cases. Eg. a user has entered some form data, and you ask: "are you sure you want to navigate away, your entered info may be lost?" or some iteration of that.Intreat
J
39

If you just want to see what link destination, you can use :

document.activeElement.href 

But getting the address line destination is not possible.

I've heard of solutions where they fire off an event if the mouse moves up to the address line (to warn the user that there are unfinished processes that have not been dealt with), but this sort of hack I would never do.

Jackshaft answered 8/1, 2014 at 11:41 Comment(0)
M
19

Kaze's answer is an interesting approach, but looking at the element focus when the page is navigated away from isn't really reliable. Partly because there is a delay between the link click and the navigation away from the page (during which time the user may move focus to some other element, but also because a link may be focused (eg. by keyboard control, or mousedown-without-click) without actually being used to navigate away from the page. So if you focused a link then closed the window, it'd think you were following the link.

Trapping onclick for every link on the page (plus onsubmit on every form) is slightly more reliable, but can still be fooled due to the delay. For example you click a link, but then before the new page starts loading hit the back button (or press Escape). Again, if you close the window it thinks you're following the link.

I want to catch the address, what the user types into the url box of the browser.

There is no way that will ever happen. It is an obvious privacy no-no.

Marsha answered 6/11, 2009 at 11:31 Comment(4)
But WHY did you do that. If your answer is not completely right, don't worry, people have the chance to judge it with their vote. But it still can be useful to other people. I would have liked to see your "last focus" approach.Idonah
Agreed - @KazenoKoe please add your answer back!Maiolica
Oh, come on, you can't expect people to view down-voting as a neutral peer review, when you build all this bling bling around it. They are intentionally trying to connect this to job searches, etc. Everyone makes mistakes, but if at all, you'd really just put your most spectacular ones on your resumee. ;-)Photomultiplier
Hehe, @Someone, yes: "If your answer is not completely right, don't worry, people have the chance to judge it with their vote." Hilarious!... :DChilson

© 2022 - 2024 — McMap. All rights reserved.