简体   繁体   中英

how to detect if webpage has changed?

Assuming I have users on a page of my site.

I update this page on my server but users will not notice the difference until reloading the page.

How can a JavaScript function detect the difference and start reloading the page ?

A solution could be using a timer which reloads the page every second but this causes a lot of traffic?

The function you are asking is to have a server which can push information to the browser like Comet .

Else, you can also create an Ajax request which will test the current version of the page displayed in the browser and verify if it wasn't updated on the server side. Once it detect that the server have a new version of the page, your script can tell it to the user (it is bad practice to reload without the user consent).

To avoid excessive call to the server, you should ensure that your Ajax request is only launch periodically (2 min ? 5 min ? More ?), and that no other request are currently running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM