How to show alert after page reload in javascript?

I am trying to make a warning window after reloading the page, but this does not work.
Please correct my code and tell me why?

$("button").click(function(){ window.location.reload(); alert("Hello world"); }); 
+7
javascript jquery
source share
4 answers

You can use sessionStorage :

 $( "button" ).click( function () { sessionStorage.reloadAfterPageLoad = true; window.location.reload(); } ); $( function () { if ( sessionStorage.reloadAfterPageLoad ) { alert( "Hello world" ); sessionStorage.reloadAfterPageLoad = false; } } ) 
+9
source share

This is called loadload. This was waaaaay before the DOM was ready, and the DOM ready was created because onload was waiting on the images.

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

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

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

Some dirty way to do this with a string ?reload in href link as 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> 
0
source share

Step 1: Write your own function for the pop-up window with the ok button (I created a parameterized function that accepts a message, warning 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. To your answer, ajax response.result == true call the AlertMessageOk function. I passed the method name to reload the 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(); } 
0
source share

All Articles