14

我试图在页面重新加载后制作警报框,但它不起作用。
请更正我的代码并告诉我为什么?

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

5 回答 5

20

您可以使用sessionStorage

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

$( function () {
        if ( sessionStorage.reloadAfterPageLoad ) {
            alert( "Hello world" );
            sessionStorage.reloadAfterPageLoad = false;
        }
    } 
);
于 2013-08-01T06:01:07.860 回答
2

这就是所谓的加载。它在 DOM ready 出现之前就已经出现了,而 DOM ready 实际上是出于 onload 等待图像的确切原因而创建的。

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

而一个 jQuery Javascript 解决方案是在 document.ready() 中绑定 window.onload 事件。

$(window).bind("load", function() {
   // code here
});
于 2013-08-01T05:59:05.620 回答
2

辉,老帖。但是我也在寻找一种解决方案,以便在单击 woocommerce 管理订单中的订单保存按钮时在重新加载后添加脚本。我认为这是使用 sessionStorage 而不是一些钩子的好方法。感谢Akhil睁开眼睛。也许有人也在看:

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');
            }
});
于 2019-02-08T22:14:55.343 回答
0

?reload一些使用链接href中的字符串的肮脏方法,如request.GETphp

<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>
于 2013-08-01T06:02:58.857 回答
0

第 1 步:使用确定按钮编写您自己的警报弹出函数(我创建了接受消息、警报类型、方法名称的参数化函数。

函数 AlertMessageOk(str, alertType, 方法)
{

     $('#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');
 }

第 2 步:在您的 ajax response.result == true 调用 AlertMessageOk 函数。我已将方法名称传递给重新加载页面。

功能按钮Activate_onClick(商店ID){

     $.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();        
 }
于 2016-04-28T13:13:16.310 回答