Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page Redirect after X seconds wait using JavaScript

I need to redirect to specific url after 5 seconds after putting an error message. First i have used Javascript as below.

document.ready(window.setTimeout(location.href = "https://www.google.co.in",5000)); 

But it is not waiting for 5 seconds. Than I searched the issue in google than come to know that "document.ready()" is invoked when document is loaded at DOM, not at web browser.

Than i have used window.load() function of jQuery but still i am not getting what i want.

$(window).load(function() {                window.setTimeout(window.location.href = "https://www.google.co.in",5000);             }); 

Can anyone please let me know how exactly i need to do to wait for 5 seconds.

like image 943
Gaurav Pant Avatar asked Jun 17 '13 14:06

Gaurav Pant


People also ask

How do I redirect a page after 5 seconds?

To redirect a webpage after 5 seconds, use the setInterval() method to set the time interval. Add the webpage in window. location. href object.

How do I redirect after delay?

If you want to redirect a page automatically after a delay then you can use the setTimeout() method in JavaScript and it will call the above property after a time delay. The solution is very simple. The setTimeout() method will call a function or execute a piece of code after a time delay (in millisecond).

How do I automatically redirect to another page?

It happens due to page redirection. To redirect from an HTML page, use the META Tag. With this, use the http-equiv attribute to provide an HTTP header for the value of the content attribute. The value of the content is the number of seconds; you want the page to redirect after.

How can I redirect after Toastr notification?

See the 'callbacks' section of the documentation: github.com/CodeSeven/toastr. Secondly, making an AJAX request then showing a notification is redundant if you're going to redirect anyway. Instead of making a single request you've made two and held up your user while they waited for the notification to go away.


2 Answers

It looks you are almost there. Try:

if(error == true){      // Your application has indicated there's an error     window.setTimeout(function(){          // Move to a new location or you can do something else         window.location.href = "https://www.google.co.in";      }, 5000);  } 
like image 130
ajtrichards Avatar answered Sep 25 '22 17:09

ajtrichards


You actually need to pass a function inside the window.setTimeout() which you want to execute after 5000 milliseconds, like this:

$(document).ready(function () {     // Handler for .ready() called.     window.setTimeout(function () {         location.href = "https://www.google.co.in";     }, 5000); }); 

For More info: .setTimeout()

like image 29
palaѕн Avatar answered Sep 25 '22 17:09

palaѕн