How would I go about overriding/catching a javascript redirect?
For example:
window.location.href="http://example.com";
The value "http://example.com" would be caught and maybe altered.
How would I go about overriding/catching a javascript redirect?
For example:
window.location.href="http://example.com";
The value "http://example.com" would be caught and maybe altered.
AFAIK you can catch this event but you cannot alter the target url. You could only prompt the user if he wants to cancel the redirect. For this you could subscribe to the before unload event:
window.onbeforeunload = function (e) {
var e = e || window.event;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'Are you sure you want to leave the site?';
}
// For Safari
return 'Are you sure you want to leave the site?';
};
You could try using __defineSetter()__
but it may not work on host objects.
Find all code that uses this:
window.location.href="http://example.com";
and change it to this:
openURL("http://example.com");
Then, implement this function where you can control the redirect however you would like:
function openURL(url) {
// examine url and decide if you want to modify it
window.location = url;
}