views:

1200

answers:

2

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.

+4  A: 

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.

Regards,

M.

Michel van Engelen
This is wrong. See my answer.
Kaze no Koe
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?
Michel van Engelen
+2  A: 

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.

bobince
You're right, I removed my answer.
Kaze no Koe