Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show an alert after reloading the page in JavaScript?

I am trying to make alert box after page reloaded, but it doesn't work.
Please correct my code and tell me why?

$("button").click(function(){
    window.location.reload();
    alert("Hello world");
});
like image 884
lyhong Avatar asked Aug 01 '13 05:08

lyhong


People also ask

How do I show notifications after page refresh?

How do you pop up an alert box when the browsers refresh button is clicked? You can do it like this: window. onbeforeunload = function() { return “Data will be lost if you leave the page, are you sure?”; };

How do you show warnings in JavaScript?

One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen. Before this function can work, we must first call the showAlert() function. JavaScript functions are called in response to events.

How do you create an alert popup in JavaScript?

JavaScript Popup Boxes: Summary If you want to display an alert of some kind to a user, use alert() . If you want to display a confirmation box with option buttons, use confirm() . If you want to use a pop up with an input and option buttons, which can display inputed values, use prompt() .


2 Answers

Hui, a old post. But i´m looking too for a solution to add a script after reload when click a order save button in woocommerce admin order. I think it is a great way to use the sessionStorage and not some hooks. Thanks to Akhil for open the eyes. Maybe some one looking too:

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');
            }
});
like image 159
Rocco Avatar answered Oct 13 '22 09:10

Rocco


That's called onload. It came waaaaay before DOM ready was around, and DOM ready was actually created for the exact reason that onload waited on images.

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

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

$(window).bind("load", function() {
   // code here
});
like image 45
Michael Unterthurner Avatar answered Oct 13 '22 10:10

Michael Unterthurner