Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page refresh in case of javascript errors

I have some pages running javascript for displaying updates of different measure values. These stuff seems to run fine. But - there is always the risk that javascript can crash in case of a fault (especially after running some hours)

So I want to implement a simple kind of watch dog. One of my ideas is to use a meta-refresh-tag. The Browser will reload the site after xy minutes and all javascripts will be reinitalized.

Of course I do not want to refresh the site if no error occured and want to reset the refresh timer using javascript. As long js runs the timer will be resetted periodally. If javascript crashs the meta-refresh-timer counts down to zero and the page will be reloaded.

Reading stackoverflow postings I have found some answers that says a reset of the meta-timer by javascript is not possible. Do you know a better way to implement the watchdog? Using javascript ittself for refreshing is useless: If the script crashs it will never fire a reload event..

like image 562
Fert Avatar asked Feb 09 '13 11:02

Fert


People also ask

Can you refresh page JavaScript?

You can use the location. reload() JavaScript method to reload the current URL. This method functions similarly to the browser's Refresh button. The reload() method is the main method responsible for page reloading.

What happens when a page refreshes?

In general, refresh is a way of describing reloading or updating what is being displayed or stored. For example, if you are on a web page, refreshing the page displays the most recent content published on that page. Essentially, you're asking the site to send your computer the newest version of the page you're viewing.

How can I refresh a page with jquery?

Method 1: Using the location. reload(): The location. reload() method reloads the current web page emulating the clicking of the refresh button on the browser.


1 Answers

Ideally you should properly handle the error instead of just reloading the page. However you could use the window.onerror event like this:

window.onerror = function() {
    location.reload();
}

More information about onerror here:

https://developer.mozilla.org/en/docs/DOM/window.onerror

like image 165
lostsource Avatar answered Sep 20 '22 12:09

lostsource