Thursday, 5 May 2016

Refresh (reload) a page once using jQuery?



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 functionality.



Please, note: replace() is not allowed due to third-party restrictions.


Answer



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 three 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 five seconds


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



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...