views:

337

answers:

2

I use a Firefox plugin that can refresh the browser window every X seconds. As a frontend developer this is really useful as I can get instant feedback on CSS / XHTML changes the moment I save them in my editor.

I've noticed, however, that this often stops working. I'm guessing this may be due to javascript/jQuery that I've added to the page interfering with the plugin.

I was just wondering if it was possible to add a temporary line of javascript to mimic this auto-refresh behaviour when needed.

+8  A: 

The easiest and hackiest solution to refreshing the page is to add this inside the head:

<meta http-equiv="refresh" content="30" />

to refresh it every 30 seconds.

You can do similar with Javascript by doing:

setTimeout('window.location.href=window.location.href;', 30000);

Note: There are several methods of reloading the page in Javascript so these will also work:

setTimeout('window.location.reload();', 30000);

and

setTimeout('history.go(0);', 30000);

and others.

Both of these will completely reload the page every 30 seconds. That's fine if all you're doing is something quick and dirty. Generally though for something users will use, you'll want to do AJAX refreshes to parts of the page instead. For example:

setInterval(refresh_table, 30000);

function refresh_table() {
  $("#table_container").load("/load_table");
}
cletus
Can't that just be location.reload() instead?
karim79
There are many ways of reloading the page. Mine is admittedly rather old school. See http://www.mediacollege.com/internet/javascript/page/reload.html
cletus
<meta http-equiv="refresh" content="600"> is the easiest solution for automatically refreshing a page.
Steven
and why not `setInterval(refresh_table,30000);` ?
gnarf
Cheers cletus, interesting to see how many methods there are!
james6848
+5  A: 

setTimeout("location.reload(true);", timeoutPeriod);

Mr. Smith
This works well for me. If you also add a reference to an appropriate id within the page in the url, e.g. ...index.html#footer you can keep the area currently being worked on viewable on refresh. Nice.
james6848