devxlogo

Redirecting to Another URL with setTimeout

Redirecting to Another URL with setTimeout

Most of Web developers may be familiar with the META REFRESH tag in HTML. This tag is used to take the user to another location automatically, after a specified delay. In the example given below, the browser automatically navigates to ‘newpage.html’, 5 seconds after the page is loaded.

  

In some cases, however, you might want to take the user to another page, perhaps depending on some actions and a varying time delay. In this context the META REFRESH tag cannot be used because it is dependent on the time when the page was loaded. The following JavaScript code snippet helps to achieve this feature using the setTimeout function.

 setTimeout('window.location.href="newpage.html"', 10000); 

This statement, when called during the specific event or action, navigates the user automatically to “newpage.html” after a delay of 10 secs (10000 milliseconds).

devx-admin

Share the Post: