lyhong lyhong - 7 months ago 29
Javascript Question

How to show an alert after reloading the page in JavaScript?

I am trying to make alert box after page reloaded, but it doesn't work.

Please correct my code and tell me why?

$("button").click(function(){
window.location.reload();
alert("Hello world");
});

Answer

You can use sessionStorage:

$( "button" ).click( function () {
    sessionStorage.reloadAfterPageLoad = true;
    window.location.reload();
} );
$( function () {
    if ( sessionStorage.reloadAfterPageLoad ) {
        alert( "Hello world" );
        sessionStorage.reloadAfterPageLoad = false;
    }
} )