How do you reload,refresh a page through Jquery, Javascript code. Having trouble finding the answer, this tutorial is for you.
How to Reload,Refresh Page through Jquery, Javascript
To reload a page with jquery use location.reload() method. By default location.reload() refresh the page from cache.
Syntax –
1 |
location.reload(); |
If you want your browser to reload the page from server instead of cache then true forceget parameter which is false by default.
1 2 3 |
// Reload page from server instead of cache. location.reload(true); |
Difference between window.onload and document.ready.
Let’s check some example –
Refresh, Reload page on the click of button
Create one button with id click.
1 |
<input type="button" id="click" name="button" value="refresh" /> |
One the click of button, reload or refresh the page.
1 2 3 4 5 6 7 8 9 |
<script type="text/javascript"> /* On the click of button. */ $('#click').click(function() { location.reload(); }); </script> |
What the above code does ?
We select the button by id using jquery selector. And then we check click event on button, when someone clicks on button then our code refresh or reload the page.
Alternative Methods to Reload, Refresh the Page
Using window.location.reload() and history.go(0) method.
1 2 3 |
$('#click').click(function() { window.location.reload(); }); |
1 2 3 |
$('#click').click(function() { history.go(0); }); |
go method loads specific page from history list. So if i write history.go(-1) it loads one page backward.
To load current page we use history.go(0) . 0 is passed for current page.