How to Automatic Refresh a web page in fixed time?

Window location.reload()

The reload() method does the same as the reload button in your browser.

Syntax

Parameters

Return Value

Browser Support

location.reload() is supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Обновить страницу с помощью JS / HTML / PHP

JS -метод location.reload() перезагружает текущую вкладку браузера и действует также как кнопка «Обновить страницу».

Пример перезагрузки страницы кликом на ссылку или кнопку:

Цикличное обновление страницы с задержкой

В коде используется тот же location.reload() выполняемый с задержкой setTimeout() в тридцать секунд.

Перезагрузка страницы с задержкой

В случаях когда после клика на кнопку или ссылку нужно перезагрузить страницу с задержкой, например две секунды:

 Обновить страницу через 2 секунды  

Пример:

Перезагрузка страницы с подтверждением

Чтобы пользователь мог подтвердить действие, можно применить метод вызова диалогового сообщения confirm.

if (confirm('Вы действительно хотите обновить страницу?'))

Пример:

Обновление родительской страницы из IFrame

Для обращения к ресурсам родительской страницы из IFrame используется объект parent , подробнее в статье «Как обновить iframe».

Перезагрузка страницы с помощью HTML

Добавление мета-тега в страницы заставит её перезагрузится. Значение атрибута content больше нуля задает задержку в секундах.

Перезагрузка страницы из PHP

Обновить страницу прямо с сервера можно c помощью функции header() , отправив заголовок « Refresh: 5 », где значение «5» указывает интервал в пять секунд.

Важно, чтобы перед вызовом функции не было отправки контента в браузер, например echo .

Источник

How to Reload the Page

[Reload All By Itself]
[Reload From A User’s Click]
[Use the Full URL?] I started to get email asking for this effect right around the time Internet stocks took off. People wrote asking how the stock sites got their pages to reload all by themselves.

The answer is pretty easy so I usually just wrote and answered in the email. While going through my notebook of possible topics, this one came up as one that is asked about a fair amount so I thought I’d write up a quick tutorial.

The trick to reloading the page is to force the browser to not look into the cache, but rather to again make a connection to the Web and bring up the document from the server. Most people know it can be done by hand by holding the shift key and clicking the “Refresh” (on IE) or “Reload” (on Navigator) buttons. If you didn’t know…now you do.

That’s a proven method but it’s not exactly very pretty to have text asking the user to hold and click. You want your site to do the trick either by itself, or by offering a method whereas the user simply clicks and the browser does it for them.

Below I have two methods. One will reload every so many second all on its own and the other will reload when the user asks for it.

Reload All By Itself

This one’s nice and easy. I’ll give you the code. Copy and paste it into the document you wish to reload. Once in there, change the number of seconds you wish the page to wait before starting the reloading process. This code goes in between the HEADtags.

Right now, the command is set to reload every 15 seconds. I checked a couple of online sites and they were all set about the same. I found the sites displaying stock information were set to around five minutes or 300 seconds.

I don’t have a refresh on this page because the darn thing would just keep refreshing and there’s nothing on this page that will update.

Reload From A User’s Click

I’ve seen this done a number of ways, but this is my favorite because it, again, forces the browser to load from the server. It is true that pages can become cached if they are reloaded a great many times, but I have had pretty good success with this. Try it:

Click to refresh the page Rather than using a refresh command, I like to go to the history of the page and set it to zero. The zero is the current page since in JavaScript, lists (arrays) are numbered starting with zero. Here’s the same effect in a button:

Use the Full URL?

Again, it’s possible that a page using the methods shown above can get cached and can stop reloading from the server. A Webmaster friend told me that if you simply set up a link to the current page but use the entire URL, the page would always reload from the server because the request starts at the domain. For example, the full URL of this page is:

http://www.htmlgoodies.com/tutors/refresh.html If you use that full URL in each of the elements above, you’ll lessen the chance the page will cache. Thus, the Meta Refresh would become:

You would then change out the JavaScript formats above to simply go to the URL rather than looking at the history file. In fact, you could lose the JavaScript altogether and just make a simple A HREF link right to the current page. The trick is to use the full URL address so that the process starts at the very beginning.

Again, the effect is the same as you’ll get with the code above. It just lowers the chance of the page getting stuck in cache.

That’s That

Short, sweet and simple. The effect is useful if done correctly. Two methods of using the effect incorrectly are refreshing so that a counter increases and refreshing so that a new banner ad displays. You don’t want to refresh your page unless there’s a very good reason. There are programs out there that update banners without refreshing the entire page. The use of the refresh to display new counter numbers is just silly. If you find this tutorial too confusing, consider utilizing Bootstrap.

And yes, I have seen both usages or I wouldn’t have thought to bring them up.

Источник

How to Automatic Refresh a web page in a fixed time?

We can auto-refresh a web page by either using a “meta” tag with the “http-equiv” property, or by using the setInterval() browser API. Automatic refreshing websites have certain use cases to them, for example, while creating a weather-finding web application, we may want to refresh our website after a set interval of time to show the user the near-exact weather data for a location.

Let’s look at the 2 approaches below to understand how to set up an auto-refreshing website.

Approach 1

In this approach, we will use the “http-equiv” property of a “meta” tag to refresh our web application after a particular interval of time which is passed in its “content” attribute. The meta tag is provided to us by default in the HTML5 specification.

Syntax

Here, “n” in a positive integer that refers to the number of seconds to refresh the page after.

Example

In this example, we will use the “http-equiv” property of a “meta” tag to refresh our web application after every 2 seconds.

    

How to Automatic Refresh a web page in fixed time?

Approach 2

In this approach, we will use the “setInterval()” API is given to us by the browser, which allows us to run a certain piece of code after every certain amount of time passed, both of which are passed as arguments to the browser API.

Syntax

setInterval(callback_fn, time_in_ms)

“setInterval()” takes 2 parameters, first is a callback function that triggers after the delay, and the second is the delay provided in milliseconds.

Example

In this example, we will use the “setInterval()” browser API to refresh our web application after every 2 seconds.

     

How to Automatic Refresh a web page in fixed time?

Conclusion

In this article, we learned how to automatically refresh our web application after a fixed amount of time using both, HTML5 and javascript using two different approaches. In the first approach, we used the “http-equiv” property of the “meta” tag, and in the second approach, we used the “setInterval” browser API.

Источник

Читайте также:  Подключение базы данных phpmyadmin php
Оцените статью