I am using a Javascript redirect to redirect visitors after X seconds. However, there is also a link on the page that takes the visitor to the same page. I would like to stop the redirection if the visitors clicks on the link first.
Please note that ,the link is opening in the new window when clicked but the javascript redirect opens in the same window.
This is the code am using in my HTML Page ,
Javascript redirect code:
<script>
<!--
timeout = '5000'; // milliseconds/1000th of a sec
window.onload = setTimeout(myRedirect, timeout); // ensure we load the whole page
function myRedirect() {
window.location = "http://www.mypage.com/page1.html";
}
//-->
</script>
and there is this link which opens in new window:
<a href="http://www.mypage.com/page2.php" target="_blank">Click Here</a>
any ideas on how i can achieve this?
window.onload = setTimeout(myRedirect, timeout);
does not ensure the whole page has been loaded. Basically, what are you doing here is: setting the timeout immediately when the browser processed your JS code, and then storing the timer id in thewindows.onload
. The correct implementation would bewindow.onload = function() { setTimeout(myRedirect, timeout) };