Javascript window events

onbeforeunload function in JavaScript | Creative Technocrayts

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";
}
JavaScript window events example

UI Practice Series

Explore more JavaScript and UI tutorials:

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

Popular posts from this blog

Best AI Tools for Women in 2025

A Complete Guide to CSS Flexbox: Layout, Alignment, and Flex Properties

Datatables.net - Plugin options