1

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?

ogside
  • 11
  • 2
  • 2
    What is the back end? If it is an asp.net back end, signalr may help you: http://signalr.net/ – mbx-mbx Mar 16 '15 at 15:30
  • well you need an endpoint that answers if the page hase changed, and you ask it via ajax and if it returns true you reload the page – john Smith Mar 16 '15 at 15:30
  • Assuming you're using server-side code, look at AJAX. If you're using ASP.NET, then I can recommend [SignalR](http://signalr.net/) for websockets – freefaller Mar 16 '15 at 15:31
  • No, I'd like only user-side code, that's because I think javascript is a good thing – ogside Mar 16 '15 at 15:44
  • Have a look at [this answer](http://stackoverflow.com/a/5748207/930393), you might be able to get something from that – freefaller Mar 16 '15 at 15:55

1 Answers1

0

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.

vdubus
  • 446
  • 3
  • 16
  • Coment wouldn't fit cause I want client-side code. But your other solution sounds good : what type of Ajax request which will test if the server have a new version of the page without reloading it all ?. (In my case it is not a problem to reload without the user consent ). – ogside Mar 16 '15 at 17:02
  • You could connect to your server with a Ajax Get request with passing in argument the current version of your page, and the server will respond that there is or not a new version of your page. So you will have to manage a code (you can maybe manage with the current URL of your page) and a version to identify a page, and you will have to create an URL to connect to it in Ajax to request your server. – vdubus Mar 17 '15 at 13:01