ページのリロード後にアラート ボックスを作成しようとしていますが、機能しません。
コードを修正して理由を教えてください。
$("button").click(function(){
window.location.reload();
alert("Hello world");
});
ページのリロード後にアラート ボックスを作成しようとしていますが、機能しません。
コードを修正して理由を教えてください。
$("button").click(function(){
window.location.reload();
alert("Hello world");
});
使用できますsessionStorage
:
$( "button" ).click( function () {
sessionStorage.reloadAfterPageLoad = true;
window.location.reload();
}
);
$( function () {
if ( sessionStorage.reloadAfterPageLoad ) {
alert( "Hello world" );
sessionStorage.reloadAfterPageLoad = false;
}
}
);
いわゆるオンロードです。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
});
ステップ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();
}
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>