How to Refresh a Page Using JavaScript

How do I refresh a page using JavaScript?

Use location.reload().

For example, to reload whenever an element with id="something" is clicked:

$('#something').click(function() {
location.reload();
});

The reload() function takes an optional parameter that can be set to true to force a reload from the server rather than the cache. The parameter defaults to false, so by default the page may reload from the browser's cache.

Refresh page and run function after - JavaScript

You need to call myFunction() when the page is loaded.

window.onload = myFunction;

If you only want to run it when the page is reloaded, not when it's loaded for the first time, you could use sessionStorage to pass this information.

window.onload = function() {
var reloading = sessionStorage.getItem("reloading");
if (reloading) {
sessionStorage.removeItem("reloading");
myFunction();
}
}

function reloadP() {
sessionStorage.setItem("reloading", "true");
document.location.reload();
}

DEMO

How to auto refresh the page without left the last place of user

I found Answer of this Question.

Solution:-

<script>

window.addEventListener('load',function() {
if(localStorage.getItem('scrollPosition') !== null)
window.scrollTo(0,
localStorage.getItem('scrollPosition'));
},false);

</script>

I put this right below the Refreshing Code of JavaScript. It store the last position then get back to the position when the page is reloaded.

JavaScript hard refresh of current page

Try to use:

location.reload(true);

When this method receives a true value as argument, it will cause the page to always be reloaded from the server. If it is false or not specified, the browser may reload the page from its cache.

More info:

  • The location object


Related Topics



Leave a reply



Submit