0

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?

1
  • The solution was already written in the "answers" section, but i wanted to point out on a error in your code. 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 the windows.onload. The correct implementation would be window.onload = function() { setTimeout(myRedirect, timeout) };
    – penartur
    Feb 24, 2012 at 7:03

3 Answers 3

3

store your timeout call in a variable.

var timeout = '5000';
var myTimeout;
window.onload = function(){
    myTimeout = setTimeout(myRedirect, timeout);
}

and when you want it cancelled, clear that timeout in that variable:

clearTimeout(myTimeout)
0

You just need to clear the timeout when the link is clicked:

<a href="somepage.html" onclick="javascript:clearTimeout(t);">click here</a>

Note that the object 't' must be what is returned from the setTimeout function. So now that will look like:

var t;

window.onload = function() {
    t = setTimeout(myRedirect, timeout);
}

Also, I may point out here that what you are doing here is generally not recommended (there definitely are rules to this exception). It would be better to use HTTP headers for the redirect, but since I don't know what exactly your requirement is, I can't comment.

4
  • 1
    a return false statement would stop the link processing i.e. then not only will the timer be cleared, but also redirection due to clicking the link also will be cancelled. Feb 24, 2012 at 7:16
  • Thanks @RohanPrabhu, I tried combining your and joseph's codes to come up with a solution but it seems i am missing something here. The page doesnt redirect anymore (considering i didnt click on the link) <script> <!-- var timeout = '5000'; var myTimeout; window.onload = function(){ myTimeout = setTimeout(myRedirect, timeout); } window.location = "mypage.com/page1.html"; } //--> </script>
    – vinny
    Feb 24, 2012 at 8:07
  • Shouldn't there be the myRedirect function? Looks like it isn't declared at all. The window.location = "wherever" should be enclosed within the myRedirect function. Feb 24, 2012 at 9:18
  • @RohanPrabhu I got it now. Including function myRedirect() { resolved the issue now. Thanks for your help.
    – vinny
    Feb 24, 2012 at 9:46
0

You should cancel your setTimeout by using clearTimeout, see example here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.