Refresh (reload) a page once using jQuery?

JqueryRefreshReload

Jquery Problem Overview


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.

Jquery Solutions


Solution 1 - Jquery

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.

Solution 2 - Jquery

To reload, you can try this:

window.location.reload(true);

Solution 3 - Jquery

window.location.href=window.location.href;

Solution 4 - Jquery

Use this:

    <script type="text/javascript">
        $(document).ready(function(){

            // Check if the current URL contains '#'
            if(document.URL.indexOf("#")==-1)
            {
                // Set the URL to whatever it was plus "#".
                url = document.URL+"#";
                location = "#";

                //Reload the page
                 location.reload(true);
            }
        });
    </script>

Due to the if condition, the page will reload only once.

Solution 5 - Jquery

$('#div-id').click(function(){
   		   				    
   location.reload();
      	});

This is the correct syntax.

$('#div-id').html(newContent); //This doesnt work

Solution 6 - Jquery

You don't have a jQuery refresh function, because this is JavaScript basics.

Try this:

<body onload="if (location.href.indexOf('reload')==-1) location.replace(location.href+'?reload');">

Solution 7 - Jquery

Use:

<html>
    <head>
        <title>Reload (Refresh) Page Using Jquery</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $('#reload').click(function() {
                    window.location.reload();
                });
            });
        </script>
    </head>
    <body>
        <button id="reload" >Reload (Refresh) Page Using</button>
    </body>
</html>

Solution 8 - Jquery

Add this to the top of your file and the site will refresh every 30 seconds.

<script type="text/javascript">
    window.onload = Refresh;

    function Refresh() {
        setTimeout("refreshPage();", 30000);
    }
    function refreshPage() {
        window.location = location.href;
    }
</script>

Another one is: Add in the head tag

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

Solution 9 - Jquery

 - location = location
 - location = location.href
 - location = window.location
 - location = self.location
 - location = window.location.href
 - location = self.location.href
 - location = location['href']
 - location = window['location']
 - location = window['location'].href
 - location = window['location']['href']

You don't need jQuery for this. You can do it with JavaScript.

Solution 10 - Jquery

Try this code:

$('#iframe').attr('src', $('#iframe').attr('src'));

Solution 11 - Jquery

Use this instead

function timedRefresh(timeoutPeriod) {
    setTimeout("location.reload(true);",timeoutPeriod);
}

<a href="javascript:timedRefresh(2000)">image</a>

Solution 12 - Jquery

For refreshing page with javascript, you can simply use:

location.reload();

Solution 13 - Jquery

Try this:

<input type="button" value="Reload" onClick="history.go(0)">

Solution 14 - Jquery

You may need to use this reference along with location.reload() check this, it will work.

this.location.reload();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeteView Question on Stackoverflow
Solution 1 - JqueryBenView Answer on Stackoverflow
Solution 2 - JqueryMilliView Answer on Stackoverflow
Solution 3 - JqueryMazView Answer on Stackoverflow
Solution 4 - JqueryParag GaiwkadView Answer on Stackoverflow
Solution 5 - JqueryswapnilView Answer on Stackoverflow
Solution 6 - JqueryAdrian P.View Answer on Stackoverflow
Solution 7 - JqueryVickyView Answer on Stackoverflow
Solution 8 - JqueryParesh3489227View Answer on Stackoverflow
Solution 9 - JquerySteveView Answer on Stackoverflow
Solution 10 - JqueryKossView Answer on Stackoverflow
Solution 11 - JqueryarunavView Answer on Stackoverflow
Solution 12 - JqueryFelipe LeãoView Answer on Stackoverflow
Solution 13 - Jqueryuser1016195View Answer on Stackoverflow
Solution 14 - JqueryTino Jose ThannipparaView Answer on Stackoverflow