Open In App
Related Articles

HTML onbeforeunload Event Attribute

Improve Article
Save Article
Like Article

The onbeforeunload event run when the document is about to be unloaded. This event is used to allow to display a message in a dialog box to inform the user to he/she wants to stay or leave the current page.

Supported Tags 

  • <body>


<element onbeforeunload = "script">

Attribute Value: This attribute contains a single value script and it runs when onbeforeunload event is called. This event attribute is supported by <body> tag.



<!DOCTYPE html>
    <title>onbeforeunload attribute</title>
        body {
        h1 {
        a {
<body onbeforeunload="return myFunction()">
    <h2>onbeforeunload attribute</h2>
    <a href=
        Go to GeeksforGeeks
        function myFunction() {
            return "This document is ready to load";


Supported Browsers: The browser supported by onbeforeunload attribute are listed below: 

  • Google Chrome 1
  • Edge 12
  • Firefox 1
  • Safari 3
  • Opera 12

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 04 Aug, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials