Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't this small redirect working (javascript)?

Tags:

javascript

function redir(){
setTimeout(window.location = '/SV/main/main.html', 10);
}

I dont know if the delay is in miliseconds or seconds, but I have tried BOTH. (by adding three zeros).

Problem is, the redirect is made right away, without any delay at all... why?

Thanks

BTW its called like this: <body onload="redir();">


1 Answers

You have to put your javascript statement between quotes :

function redir(){
    setTimeout("window.location = '/SV/main/main.html';", 10);
}

The delay is in milliseconds btw.

As said in the comments and other answers, it is much cleaner to use an anonymous function to do such things :

function redir() {
  setTimeout(function(){
    window.location = "/SV/main/main.html";
  }, 10); // 10 milliseconds
}
like image 78
Wookai Avatar answered Apr 14 '26 15:04

Wookai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!