1

How can I force to redirect the page even if the alert box button is not pushed?

I have code like this:

<script type="text/javascript">
function Redirect()
{
window.location="../index.php";
}
setTimeout('Redirect()', 5000);
alert("You will be redirected to a new page in 5 seconds"); 
window.location="../index.php"; 
</script>

Now I have to push the button to be redirected. What I want to do is: if visitor doesn't push OK button in 5 seconds he's forced to ../index.php and if he push OK button system redirects him immediately to ../index.php

PhpNewbie
  • 89
  • 1
  • 9

2 Answers2

0

you cant achieve it with alert or confirm or prompt because all of these freezes the browser so further Javascript will not execute. use this

setTimeout('Redirect()', 5000);
function Redirect()
{
window.location="../index.php";
}
<div class="alert">
  
  Your text
  <button class="closebtn" onclick="Redirect();">OK</button> 
</div
><style>
.alert {
    padding: 20px;
    background-color: #f44336;
    color: white;
}
</style>
jasinth premkumar
  • 1,430
  • 1
  • 12
  • 22
0

Just Remove the single quotes from the function name

function Redirect()
{
window.location="../index.php";
}
setTimeout(Redirect(), 5000);
Sreejesh K Nair
  • 563
  • 1
  • 6
  • 16