I'd like to know which is the location the page is trying to navigate (via Javascript) to and if it is http://foo.com
I'd like to abort the redirection.
Example:
function onredirect(url)
{
if(url == 'http://foo.com')
{
abort();
}
}
I couldn't find a way to either handle the action let alone getting the target-url in an event handling parameter. Is it possible to :
- Listen to page changed event (as a result of
window.location.href
command) and - Abort navigation if a specific URL is the target?
The intention is that the snippet below does not navigate to the specified website:
function handleNavigation(urlDestination)
{
alert('leaving to ' + urlDestination);
alert('Now a code for abortion should be executed if URL is foo.com');
}
window.onhashchange = handleNavigation;
window.onunload = handleNavigation ;
window.addEventListener("beforeunload", function (e) {
handleNavigation()
}, false);
window.addEventListener("unload", function (e) {
handleNavigation()
}, false);
window.addEventListener('popstate', handleNavigation);
window.onload = function(){
window.location.href = 'http://foo.com'; //I want to know when this happened
}
<body onunload="handleNavigation()">
</body>
The answer to this question will help me answer Javascript Injection prevention on Wordpress .
What I want is that if the user types http://foo.com
he can leave. HOwever, if a script redirects him to http://foo.com
, I want to protect him from leaving to that place.
Although this is similar to Event when window.location.href changes I want to see the target-url as a parameter. This could also be useful I wanted to log the pages the users are going to after leaving my website.