Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript onload and onunload

Consider the following HTML snippet containing some javascript utilizing prompt and unload. The prompt() method works fine but I want alerting something like Goodbye, user when reloading or leaving the page. Any help is greatly appreciated.

<body onload="promptName()" >


        <script type="text/javascript">
        function promptName()
        {
            var userName = prompt("What's your name ?", "")
            return userName;
        }

        function goodBye()
        {
            alert("Goodbye, " + promptName() + "!");
        }

        window.onunload = goodBye();

        </script>

  </body>
like image 233
George Avatar asked Mar 28 '12 12:03

George


People also ask

What is onload and Onunload?

The onunload utility, which unloads data from a database, writes a database or table into a file on tape or disk. The onload utility loads data that was created with the onunload command into the database server.

What is Onunload in JavaScript?

The onunload event occurs once a page has unloaded (or the browser window has been closed). onunload occurs when the user navigates away from the page (by clicking on a link, submitting a form, closing the browser window, etc.).

What is Onbeforeunload in JS?

The onbeforeunload event occurs when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page. The default message that appears in the confirmation box, is different in different browsers.


4 Answers

You should write it like this:

window.onunload = goodBye;

Also, you might consider using the onbeforeunload event in some browsers:

window.onbeforeunload = goodBye;

When you write window.onunload = goodBye(); you assign whatever handler that is returned from goodBye to the unload event. Since nothing is returned, there will be no event handler. You need to reference the function instead: window.onunload = goodBye;

like image 200
David Hellsing Avatar answered Oct 03 '22 02:10

David Hellsing


You can't assing this way: window.onunload = goodBye();

If you want to assing this way you have three ways:

// by this way you use the name of the function, so you override the onunload function with goodBye function
window.onunload = goodBye;

or

  // This way you redefine the function
    window.onunload = function(){goodBye()};

And my favourite because it allows you to add more functionality:

// This way you create a event listener which allows you to add as many functions as you ant
window.addEventListener("unload", goodBye, false); 
like image 22
Javier Cobos Avatar answered Oct 03 '22 02:10

Javier Cobos


As seen HERE

window.onbeforeunload = function() {
    alert("Goodbye, " + promptName() + "!");
};

or

window.onbeforeunload = goodBye;

Although I would suggest saving the username to a glob by seting a var outside you load func that receives the username after first prompt, then you dont have to prompt them for their name again when they leave

like image 28
SpYk3HH Avatar answered Oct 03 '22 04:10

SpYk3HH


<body onload="promptName()" >


        <script type="text/javascript">
        function promptName()
        {
            var userName = prompt("What's your name ?", "")
            return userName;
        }



        window.onbeforeunload = function() {
    alert("Goodbye, " + promptName() + "!");
}
        </script>

  </body>

onbeforeunload

like image 21
PraveenVenu Avatar answered Oct 03 '22 02:10

PraveenVenu