How To Cancel Browser Navigation That Is Already In Progress With Javascript
I don't believe this is possible but thought I'd give it a shot. As part of a project that I'm working on, there is a page on a different site that a user can navigate to via a sta
Solution 1:
I conducted a simple test, it is possible. I used PHP to conduct the test but this will not be an issue if you use a different language. The bottom line is JavaScript can do it as seen in cancel.php
. Please see the sample code:
sleep.php
<?php
sleep(30);
echo'hello';
?>
cancel.php
<h1>hello?</h2><scripttype="text/javascript">
location.href='sleep.php';
setTimeout(function(){
location.href='#';
alert('it is taking too long to respond, probably the site is down!');
}, 10000);
</script>
After running cancel.php, it will redirect to sleep.php. After 10 seconds of loading time, it will stop loading sleep.php and conclude that the site is down.
Solution 2:
You can redirect to somewhere else, while the current DOM is still alive.
I ran this simple test, and it redirects to yahoo.com
$("#btn").click(function() {
document.location.href = "http://www.google.com";
document.location.href = "http://www.yahoo.com";
});
I havent tested with a timer, but seems like it should work, although I cant be sure 100%. Please post your findings!
Post a Comment for "How To Cancel Browser Navigation That Is Already In Progress With Javascript"