0

Is there any way to get the URL written with location.href into window.onbeforeunload function?

I want to include some 3rd party js into my website. Some of them might have redirection code inside of it something like this:

location.href="http://example.com";

So, it will cause a redirection to their website when user try to open my website. So I just want to prevent it. I can do that using:

window.onbeforeunload = function () {
    // Here I want to get the URL where user is going to be redirect
    // If that URL is a 3rd party website, I will return false 
    return false;
}

As per my knowledge, it is something like impossible.

Any help would be appreciated.

PS: I already checked this: How can i get the destination url in javascript onbeforeunload event?

So here simply, I just want to prevent such automated 3rd party redirection. Is there any code/tool/script available for that?

Thanks, Parth vora

Community
  • 1
  • 1
Parth Vora
  • 4,073
  • 7
  • 36
  • 59
  • If you can't trust a third party library to not mess with your page in ways you don't intend I have to ask if it's a good idea to use that library.... This is mostly possible if you override the location property itself. – Gerrit0 Dec 06 '16 at 06:41
  • @Gerrit0, ok so how can I override location property? – Parth Vora Dec 06 '16 at 06:43
  • Sorry, looks like I was somewhat wrong about this... it may be somewhat possible to override, but there is no clean solution... http://stackoverflow.com/questions/6478617/is-it-possible-to-overwrite-window-location-function-with-a-custom-function – Gerrit0 Dec 06 '16 at 06:52

0 Answers0