简体   繁体   中英

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");
});

You can use sessionStorage :

$( "button" ).click( function () {
        sessionStorage.reloadAfterPageLoad = true;
        window.location.reload();
    } 
);

$( function () {
        if ( sessionStorage.reloadAfterPageLoad ) {
            alert( "Hello world" );
            sessionStorage.reloadAfterPageLoad = false;
        }
    } 
);

That's called onload. It came waaaaay before DOM ready was around, and DOM ready was actually created for the exact reason that onload waited on images.

window.onload = function () { 
    alert("It's loaded!");
    //dom not only ready, but everything is loaded
}

And a jQuery Javascript solution is to bind the window.onload event in document.ready().

$(window).bind("load", function() {
   // code here
});

Some dirty method of doing it with ?reload string in link href like request.GET in php

<a class="button" href="?reload/">reload and alert</a>
<script type="text/javascript">         
    args = location.search.substr(1);
    if (args=='reload/')alert('page reloaded');  
</script>

Step 1 : Write your own function for alert popup with ok button (i have created parameterized function which accept message, alert type, method name.

function AlertMessageOk(str, alertType, method)
{

  $('#AlertMessage .divDialogElements').empty(); $('#AlertMessage .divDialogElements').append(msg); if (alertType == "success") { $('#AlertMessage #modalAlertHeaderTitle').html("Success"); $('#AlertMessage #modalAlertHeaderTypeClass').attr("class", "modal-header alert-success"); } else if (alertType == "error") { $('#AlertMessage #modalAlertHeaderTitle').html("Error"); $('#AlertMessage #modalAlertHeaderTypeClass').attr("class", "modal-header alert-danger"); } else if (alertType == "info") { $('#AlertMessage #modalAlertHeaderTitle').html("Status"); $('#AlertMessage #modalAlertHeaderTypeClass').attr("class", "modal-header alert-info"); } else if (alertType == "warning") { $('#AlertMessage #modalAlertHeaderTitle').html("Warning"); $('#AlertMessage #modalAlertHeaderTypeClass').attr("class", "modal-header alert-warning"); } $('#AlertMessage #btnAlertOk').attr("onclick", method); $('#AlertMessage').modal('show'); } 

Step 2: On your ajax response.result == true call to AlertMessageOk function. I have passed method name to reload page.

function buttonActivate_onClick(storeID) {

     $.ajax({
         type: "POST",
         url: "/configuration/activateStore",
         timeout: 180000,
         data: { StoreID: storeID },
         success: function (response) {
             if (response.result == true) {
                 AlertMessageOk("Store configuration for Store ID " + storeID + " is successfully activated.", "success", "reloadPage();");
             }
         },
         error: function (xhr, textstatus) {
             AlertMessage("Error:   " + xhr.statusText + "  [" + xhr.status + "]", "error");

         }
     });
     $('#wait_load').css("display", "none");
 }


 function reloadPage() {
   location.reload();        
 }

Hui, a old post. But i´m looking too for a solution to add a script after reload when click a order save button in woocommerce admin order. I think it is a great way to use the sessionStorage and not some hooks. Thanks to Akhil for open the eyes. Maybe some one looking too:

jQuery('.button.save_order.button-primary').click(function() {

            sessionStorage.setItem('save_order',true);

});

jQuery( function () {
        if ( sessionStorage.getItem('save_order') ) {
                alert( "Hello world" );
                sessionStorage.removeItem('save_order');
            }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM