views:

1969

answers:

2

Hi! Newbie question: I'm wondering how to refresh/reload a page (or even specific div) once(!) using jQuery? Ideally in a way right after the "DOM structure" is available (cf. "onload" event) and not negatively affecting "back button" or "bookmark" functionalities. Please note: ''replace('' is not allowed due to third-party restrictions. Thanks in advance! --Pete

+4  A: 
window.location.href=window.location.href;
Maz
<irony>this code actualy doesn't use jQuery</irony> ;-)
zerkms
+1  A: 

EDIT:

Alright, i think i got what you're asking for, try this

if(window.top==window) {
    // you're not in a frame so you reload the site
    window.setTimeout('location.reload()', 3000); //reloads after 3 seconds
} else {
    //you're inside a frame, so you stop reloading
}

If it is once, then just do

$('#div-id').triggerevent(function(){
    $('#div-id').html(newContent);
});

If it is periodically

function updateDiv(){
    //get new content through ajax
    ...
    $('#div-id').html(newContent);
}

setInterval('updateDiv()', 5000); // that's 5 seconds

So, every 5 seconds the div #div-id content will refresh. Better than refreshing the whole page.

Ben
Thanks but I want the page to refresh/reload only ONCE (per browser session if possible)...?
Pete
What's the trigger for reloading? a certain event or just time?
Ben
trigger: the first "call" of the page (sorry, my english is kinda poor *g*)
Pete
Try the solution i just edited
Ben
omfgroflmao, you're a genius! The "edited" version is exactly what I was looking for (the whole day btw...). Is it possible to add an optional time trigger here? So that location.reload is executed - let's say - after 3 seconds...? TIA (YOU've really made my day!)
Pete
Yeah, sure, just change location.reload() with window.setTimeout('location.reload()', 3000);
Ben
Great! So, this is how it should work with the time trigger (for IE7 only), right?<!--[if IE 7]><script language="JavaScript">if(window.top==window) { window.setTimeout('location.reload()', 3000);} else {}</script><![endif]-->BTW: Any chance to realize this with scripting turned off (as a backup)?
Pete
Yes and no, you need JS to do this.
Ben
OK, then I'll advise my customers via <noscript> notice to turn on JS. Thanks again! You, this site - really awesome! Bye.
Pete