如何在JavaScript中重新加载页面后显示警告?

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

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

这叫做onload。 在DOM准备就绪之前,它已经过了waaaaay,并且实际创建了DOM就是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 }); 

一些脏的方法,在链接href中?reload字符串,如php中的request.GET

 reload and alert  

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

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