Ad

Call Function After Window Reload Using Same Button

- 1 answer

$('#start').click(function () {
   window.location.href=window.location.href;
   runme();
});

This is my simple goal, every time user click the start button, I want the page to reload but still call the custom rume function. Please let me know if this is possible or there are other way. Thanks in advance.

Ad

Answer

Whenever you reload the page JS run again from start, So you can't directly trigger some function after page reload. But in order to achieve this, you can use, sessionStorage
So, you can do something like:

$('#start').click(function () {
    sessionStorage.setItem('callRunMe', '1')
    window.location.href=window.location.href;
});
//On Page load 
$(document).ready(function () {
    if (sessionStorage.getItem('callRunMe')) {
        runMe();
        sessionStorage.removeItem('callRunMe');
    }
});
Ad
source: stackoverflow.com
Ad