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

前端 未结 5 894
栀梦
栀梦 2020-12-31 07:31

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(){
    wind         


        
5条回答
  •  生来不讨喜
    2020-12-31 08:28

    You can use sessionStorage:

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

提交回复
热议问题