What do I have to do to have a function on a website where it says it will redirect you to the site in 3 seconds or so?
-
2possible duplicate of [How can i redirect to a page (home) via another page(success page) where some success message is to be displayed?](http://stackoverflow.com/questions/2949301/how-can-i-redirect-to-a-page-home-via-another-pagesuccess-page-where-some-suc) – kennytm Jul 20 '10 at 16:15
-
1I'm just curious, why would you ever want to do that? Everytime I visited such page, I'd rather have been directed in 0 seconds. – allesklar Jul 21 '10 at 11:01
-
@allesklar sorry for the late reply. I'm in development of a site, and I wanted to see if there was an easier to switch from code editor to web browser without hitting refresh everytime. – codedude Aug 16 '10 at 21:59
-
This [JS redirect](http://insider.zone/tools/client-side-url-redirect-generator/) generator will help you to easily create delayed redirect snippets. It has a noscript and seo support, plus has an IE 8 - and lower - fix to pass the http referer! – Patartics Milán Sep 03 '15 at 15:27
-
1@allesklar one example to use this. If you change your domain and want everyone to be redirected to the new domain in the first month (but letting them know the old domain will be removed after a month or so). Because even if you send and email there's always someonte who forget and go to the old domain. – patricia Feb 01 '16 at 15:39
-
Possible duplicate of [How can i redirect to a page (home) via another page(success page) where some success message is to be displayed?](https://stackoverflow.com/questions/2949301/how-can-i-redirect-to-a-page-home-via-another-pagesuccess-page-where-some-su) – Teja Bhagavan Kollepara Jun 29 '17 at 11:32
7 Answers
<meta http-equiv="refresh" content="3;url=http://www.google.com/" />

- 1,023,142
- 271
- 3,287
- 2,928
-
1
-
10The W3C recommends this tag not be used:https://www.w3.org/TR/WCAG10-HTML-TECHS/#meta-element – huseyin39 Nov 13 '19 at 22:14
-
2
-
1
-
2@JohnSmith - W3C recommends that you should not guess how long a user needs to read a comment, and instead allow the user to perform the redirect instead. However the meta refresh tag is still the simplest method prevent the continued display of an page which might quickly become out of date, all other methods require the use of Javascript in some form. – Andy Henson May 29 '23 at 11:19
You're probably looking for the meta
refresh
tag:
<html>
<head>
<meta http-equiv="refresh" content="3;url=http://www.somewhere.com/" />
</head>
<body>
<h1>Redirecting in 3 seconds...</h1>
</body>
</html>
Note that use of meta
refresh
is deprecated and frowned upon these days, but sometimes it's the only viable option (for example, if you're unable to do server-side generation of HTTP redirect headers and/or you need to support non-JavaScript clients etc).

- 263,068
- 57
- 365
- 409
If you want greater control you can use javascript rather than use the meta tag. This would allow you to have a visual of some kind, e.g. a countdown.
Here is a very basic approach using setTimeout()
<html>
<body>
<p>You will be redirected in 3 seconds</p>
<script>
var timer = setTimeout(function() {
window.location='http://example.com'
}, 3000);
</script>
</body>
</html>
-
1This would be the way to go if you want the text to dynamically count down to 0 prior to the redirect. Use a 1 second timer, where the timer function updates the HTML text and then start a new 1 second timer, until 3 seconds have elapsed, then do the redirect. – Remy Lebeau Jul 20 '10 at 19:41
Here's a complete (yet simple) example of redirecting after X seconds, while updating a counter div:
<html>
<body>
<div id="counter">5</div>
<script>
setInterval(function() {
var div = document.querySelector("#counter");
var count = div.textContent * 1 - 1;
div.textContent = count;
if (count <= 0) {
window.location.replace("https://example.com");
}
}, 1000);
</script>
</body>
</html>
The initial content of the counter
div is the number of seconds to wait.

- 12,435
- 15
- 71
- 107
-
This didn't work until I replaced `location.href="https://example.com";` with `window.location='https://example.com'` – NateH06 Jul 29 '17 at 15:12
-
1Might be better to use `window.location.replace("http://example.com");` for the reasons discussed here: https://stackoverflow.com/a/506004 The change would provide this answer more generality. – Psyche May 09 '18 at 20:39
-
1If the redirect target is slow to respond (more than 1 sec) then `window.location.replace()` is called multiple time and the X second `count` can go negative. I modified two lines : `div.textContent = ((count < 0) ? 0 : count);` and `if (count == 0) {`. – ForguesR Nov 19 '21 at 19:46
Place the following HTML redirect code between the and tags of your HTML code.
<meta HTTP-EQUIV="REFRESH" content="3; url=http://www.yourdomain.com/index.html">
The above HTML redirect code will redirect your visitors to another web page instantly. The content="3; may be changed to the number of seconds you want the browser to wait before redirecting. 4, 5, 8, 10 or 15 seconds, etc.

- 2,185
- 3
- 35
- 48
Use this simple javascript code to redirect page to another page using specific interval of time...
Please add this code into your web site page, which is you want to redirect :
<script type="text/javascript">
(function(){
setTimeout(function(){
window.location="http://brightwaay.com/";
},3000); /* 1000 = 1 second*/
})();
</script>

- 5,768
- 1
- 38
- 38
-
`` is a better option as it is simpler and works without JavaScript support. – Edward Apr 06 '16 at 18:48
-
you are right bro... but its depends upon situation.. sometime we need to redirect on specific event for that JS is the best option. – Sunny S.M Apr 07 '16 at 07:06
-
I get what you mean, "Sunny S.M". Although meta tags should be used almost always, there could be specific scenarios like yours where JavaScript would be the only option. – Edward Apr 07 '16 at 10:42