Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 622
Pete Avatar asked Apr 01 '10 00:04

Pete


People also ask

How do I refresh a page only once?

hash property & without changing/adding hash(#) sign to the URL of the page. We are going to use DOM location reload() method to achieve the same.

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.

How do you refresh a page after 2 seconds?

Using location. reload() Method: This method is used to reload the current page imitating the action of the refresh button of the browser.

Can JavaScript refresh a page?

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.


1 Answers

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.

like image 72
Ben Avatar answered Sep 22 '22 01:09

Ben