85

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.

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
Pete
  • 853
  • 1
  • 6
  • 4
  • I am missing something in your question. What do you mean by "refresh" the page. If the page has loaded, reloading immediately would just load the exact same content, right? What are you trying to accomplish? – Doug Neiner Apr 01 '10 at 00:50
  • I'm working on a template which comes up "normally" first but is forced into an iFrame when refreshed/reloaded. Now I want the page to display in that iFrame on its first "call" as well. TIA! – Pete Apr 01 '10 at 01:02
  • I think you should read this article http://mycodingtricks.com/snippets/javascript/refresh-page-using-jquery/ – Shubham Kumar Jul 07 '16 at 14:08

14 Answers14

83

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ben
  • 16,275
  • 9
  • 45
  • 63
  • Thanks but I want the page to refresh/reload only ONCE (per browser session if possible)...? – Pete Apr 01 '10 at 01:05
  • What's the trigger for reloading? a certain event or just time? – Ben Apr 01 '10 at 01:05
  • trigger: the first "call" of the page (sorry, my english is kinda poor *g*) – Pete Apr 01 '10 at 01:09
  • 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 Apr 01 '10 at 01:55
  • Yeah, sure, just change location.reload() with window.setTimeout('location.reload()', 3000); – Ben Apr 01 '10 at 02:01
  • Great! So, this is how it should work with the time trigger (for IE7 only), right? BTW: Any chance to realize this with scripting turned off (as a backup)? – Pete Apr 01 '10 at 02:10
  • Yes and no, you need JS to do this. – Ben Apr 01 '10 at 02:20
  • OK, then I'll advise my customers via – Pete Apr 01 '10 at 02:30
67

To reload, you can try this:

window.location.reload(true);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Milli
  • 1,351
  • 15
  • 19
  • 1
    can you explain the bool argument? – tomfumb Nov 14 '11 at 19:27
  • 7
    The argument to the location.reload function determines if the browser should retrieve the document from the web-server. If needed to pull the document from the web-server again (such as where the document contents change dynamically) pass the argument as 'true' http://grizzlyweb.com/webmaster/javascripts/refresh.asp – Squirrel5853 Dec 29 '11 at 15:43
40
window.location.href=window.location.href;
Maz
  • 3,375
  • 1
  • 22
  • 27
5

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Parag Gaiwkad
  • 91
  • 1
  • 1
4
$('#div-id').click(function(){

   location.reload();
        });

This is the correct syntax.

$('#div-id').html(newContent); //This doesnt work
Flexo
  • 87,323
  • 22
  • 191
  • 272
swapnil
  • 41
  • 1
3

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');">
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adrian P.
  • 5,060
  • 2
  • 46
  • 47
  • Nice one. you can even put it inside a function: and function init(){ if (location.href.indexOf('reload')==-1) location.replace(location.href+'&reload');} – Adrian P. Feb 09 '12 at 23:40
2

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>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Vicky
  • 9,515
  • 16
  • 71
  • 88
1

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">
Paresh3489227
  • 845
  • 11
  • 22
1
 - 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.

Steve
  • 11
  • 1
0

Try this code:

$('#iframe').attr('src', $('#iframe').attr('src'));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Koss
  • 1,012
  • 11
  • 8
0

Use this instead

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

<a href="javascript:timedRefresh(2000)">image</a>
cereallarceny
  • 4,913
  • 4
  • 39
  • 74
arunav
  • 1
0

For refreshing page with javascript, you can simply use:

location.reload();
Ariona Rian
  • 9,153
  • 3
  • 24
  • 36
Felipe Leão
  • 915
  • 2
  • 15
  • 27
0

Try this:

<input type="button" value="Reload" onClick="history.go(0)">
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1016195
  • 153
  • 3
  • 5
  • 14
0

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

this.location.reload();
cch
  • 3,336
  • 8
  • 33
  • 61