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
Is there a way to reload page and after that inmediately open a modal?
When you call window.location.reload()
, the page is reloaded just like when you press f5 or hit enter on your url bar. This means your current page state is lost, so the execution of your code is interrupted and starts over from the beginning on the freshly loaded page.
What you could do is add a hash to your url when the user clicks the button (for example https://www.my.page/#modal
) and then check if this is present when you load the page:
//this executes when the page is ready
$(function() {
if($(location).attr('hash') === '#modal') {
$('#modal_perso').modal('show');
}
});
Note that if somebody bookmarks the page including the hash, this will also be executed.
In your edit, you are trying to use window.location.href.split('#')[0]
to remove the hash from the url. By itself, this doesn't do anything - it is just a value. You need to assign the value to something. You can not assign the value directly to window.location.href
here because this will make most browsers reload the page.
What you can do is use HTML5 history API like this for example:
window.history.pushState(null, document.title, window.location.href.split("#")[0]);
Avoid page reload and when press reload using Javascript/JQuery
You can't really prevent the user from reloading, as it is a default behavior of the browser, what you can do is store the timer value somewhere (a backend server or localstorage) and then fetch the value from there during each session
// you cannot run this snippet here because of stack security reasons
let timerValue = localStorage.getItem('timer') || 10; // if timer value is not found then it should be 10 seconds by default
const wait = (ms) => new Promise(r => setTimeout(r, ms));
run();
async function run() {
console.log('timer has begun');
while(true) {
if (timerValue <= 0) break;
timerValue--;
localStorage.setItem('timer', timerValue);
await wait(1000);
}
console.log('timer has ended');
localStorage.setItem('timer', null); // reset timer once the test is finished
}
Related Topics
All Falsey Values in JavaScript
Formatting a Number With Exactly Two Decimals in JavaScript
How to Check If an Object Has a Specific Property in JavaScript
Can't Append ≪Script≫ Element
Negative Lookbehind Equivalent in JavaScript
Get the Highlighted/Selected Text
How Does the "This" Keyword in JavaScript Act Within an Object Literal
Is JavaScript Guaranteed to Be Single-Threaded
What Does 'Return' Keyword Mean Inside 'Foreach' Function
Why Does Parseint Yield Nan With Array#Map
What Does the Construct X = X || Y Mean
How to Add a Key/Value Pair to a JavaScript Object
Access Java/Servlet/Jsp/Jstl/El Variables in JavaScript
How to Check That a Number Is Float or Integer
What Do Parentheses Surrounding an Object/Function/Class Declaration Mean
How to Interpolate Variables in Strings in JavaScript, Without Concatenation
Usage of the Backtick Character (') in JavaScript
Does Es6 Introduce a Well-Defined Order of Enumeration For Object Properties