1

I thought about getting the final output link using some listener for redirect event JavaScript like OnHashChange and then cancel the redirect if it was an 'specified link'.

https://developer.mozilla.org/pt-BR/docs/Web/API/WindowEventHandlers/onhashchange

Where OnHashCahnge return the new URL and old URL, but only for hash urls.

So I try beforeunload

https://developer.mozilla.org/pt-BR/docs/Web/Events/beforeunload

But although I can use a function before leaving the page, it does not return the new URL

  • Cancel specified links? What for beforeunload? You want the user to go to a different page even though they are closing yours? – StackSlave Jan 31 '19 at 00:31
  • JavaScript isn't allowed to cancel the redirect. All that `beforeunload` can do is return a string. The user will then be asked if they really want to leave. If they say no, then the redirect will be cancelled. – Barmar Jan 31 '19 at 01:07
  • I don't think there's any way for the `beforeunload` listener to find out where they're trying to go to. – Barmar Jan 31 '19 at 01:08

0 Answers0