0

I have a button which, when pressed, should reload the page. I use location.reload() to perform the action (following the answers on how to reload a page with JS).

The code I used (available on codepen.io)

HTML

<button class="reload">update</button>
<div class="radio"></div>

JS

$('.reload').click(function() {
  alert('clicked');
  location.reload(true);
});

window.onload = function() {
  $('.radio').text($.now());
};

The pages initially loads with the current Epoch counter but clicking the button does not update it.

I also tried to use $(document).ready() in case window.onload would not be triggered by location.reload() - same result.

How can I force a page reload in this scenario?

Community
  • 1
  • 1
WoJ
  • 27,165
  • 48
  • 180
  • 345

5 Answers5

1

I don't think you need to use jQuery to do this.

Try this:

<button class="reload" onclick="location.reload()">update</button>
<div class="radio"></div>
Ferrmolina
  • 2,737
  • 2
  • 30
  • 46
  • 1
    True, but I load jQuery anyway (for other parts of the code) and wanted to understand what I did wrong – WoJ Dec 21 '15 at 14:39
1

Try history.go(0);

$('.reload').click(function() {
  //window.location.reload(true);
  history.go(0);
});

window.onload = function() {
  $('.radio').text($.now());
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="reload">update</button>
<div class="radio"></div>
Yogesh Sharma
  • 2,017
  • 1
  • 15
  • 33
0

Try this. This may work fine:

$('.reload').click(function() {
    location.href=location.href;
});
Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80
Ketha Kavya
  • 558
  • 6
  • 18
-1

What's the issue?

$(document).ready(function() {
  $('.reload').on('click', function() {
    window.location.reload();
  })
});

https://jsfiddle.net/gzo8kr0u/

Sild
  • 3,077
  • 3
  • 15
  • 21
-1

Try this:

$( ".reload" ).click(function(ev) {  
    ev.preventDefault();  
    location.reload(); 
});
Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80
Komal
  • 2,716
  • 3
  • 24
  • 32