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 ソリューションは、window.onload イベントを document.ready() にバインドすることです。

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

ステップ1:OKボタンでアラートポップアップ用の独自の関数を作成します(メッセージ、アラートタイプ、メソッド名を受け入れるパラメーター化された関数を作成しました。

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

ステップ 2: ajax で response.result == true AlertMessageOk 関数を呼び出します。ページをリロードするメソッド名を渡しました。

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();        
 }
于 2016-04-28T13:13:16.310 に答える
0

PHPのように?reloadリンクhrefの文字列でそれを行ういくつかの汚い方法request.GET

<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 に答える