1

I know this question has been discussed many times but there does not seem to be a universal working answer.

I have a web page that displays data which is continually updated and I need to work out how I can prevent the web page from refreshing if there is no internet connection.

I have tried many different approaches but without exception, went I disable my internet connection, browsers displays

Firefox: Server not found  
Chrome: There is no internet connection  
IE: You’re not connected to the internet

I have looked into various browser plug-ins but none of them work.

I have taken a look at navigator.onLine but even using this browsers still display their own not connected pages. I have looked at using some sort of AJAX call without any success.

I do not need to worry about who is using which browser because this script is going to run on a known device configured to a known specification.

Is there a full proof method of preventing a page refresh until there is a connection?

loadDoc("example.com/comms_true.png", myFunction1);
function loadDoc(url, cFunction) {
var xhttp;
xhttp=new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
cFunction(this);
setTimeout(function() {
location.reload();
}, 30000);
} else {
}
};
xhttp.open("GET", url, true);
xhttp.send();
}

function myFunction1(xhttp) {
setTimeout, 30000;
}
DCJones
  • 3,121
  • 5
  • 31
  • 53
  • AJAX would seem to be the most reliable method. Why did it not work for you? Websockets or server side events may also be a better solution if you need to keep the data in sync with the client. – Rory McCrossan Aug 14 '17 at 15:10
  • @Rory McCrossan hi and thank you for your reply. When used Ajax or any other method and the internet was disconnected all I had was the browser telling me so. What I want is for the current data to remain displayed and only refresh when the connection is reastablished. – DCJones Aug 14 '17 at 15:13
  • Well, you can't stop someone from refreshing the page. All you can do is advise them that they appear to be offline and refreshing will have... whatever effect on your site. – Rory McCrossan Aug 14 '17 at 15:15
  • @Rory McCrossan there is no user interaction, all the script does is display data in a public area. – DCJones Aug 14 '17 at 15:16
  • So why is refreshing a concern at all? – Rory McCrossan Aug 14 '17 at 15:16
  • @Rory McCrossan because the displayed data is always changing. I notice that 'meagar' has marked this question as a duplicate, it is not although it may be asking a similar question, not the same. – DCJones Aug 14 '17 at 15:22
  • In which case I'd say that you need to change your logic. Don't refresh the page - use a technology which was designed for keeping server/UI in sync. Websockets or Server side events are exactly what you need. AJAX polling would also work, although isn't really an ideal solution, but is quicker to setup. With these methods you can implement code to execute when the request fails for any reason. – Rory McCrossan Aug 14 '17 at 15:23
  • @Rory McCrossan I will give Ajax another try. Many thanks for your time. – DCJones Aug 14 '17 at 15:25
  • @Rory McCrossan hi, sorry to be a pain. I have now used the code in my edit which I think is correct bur if I disable my comms the browser displays it's "Server not found " page. Am I doing this correctly? – DCJones Aug 14 '17 at 17:15

0 Answers0