Javascript window events
onbeforeunload function in JavaScript
When you have filled up a form in a web page and if you close the page before submitting it, you may get a message "Are you sure to leave this page?" Such customized messages are examples of window events onbeforeunload.
The onbeforeunload event is fired when the document unloads its resources. You may notice the document is still visible and the event can be cancelled. onbeforeunload event is assigned to the <body> element.
Various mobile user agents ignore the event.
HTML Code Example
You can implement the onbeforeunload event like this:
<body onbeforeunload="return myFunction()">
Other tags
</body>
JavaScript Code Example
Here’s an example of the JavaScript function to show the prompt:
function myFunction(){
return "Your message goes here";
}
UI Practice Series
Explore more JavaScript and UI tutorials:
- DOM manipulation using jQuery
- Input range slider UI and populating the value
- DOM functions and array
Feel free to share your feedback or queries, and Subscribe to Creative Technocrayts to be part of a growing community.
Follow us on Facebook: @CreativeTechnocrayts
Comments
Post a Comment