Learn html - html tutorial - Onbeforeunload attribute in html - html examples - html programs
The onbeforeunload attribute is used to specify when the document needs to be unloaded.
The onbeforeunload attribute allows the user to show a message in a confirmation dialog box to inform whether the user he/she needs to stay or leave the current page.
Hence the default message is appearing in the dialogue box. It is based on the different browsers.
We cannot remove the message but we can add a custom message in the dialogue box.
Sample Coding for onbeforeunload Attribute in HTML:
Tryit<!DOCTYPE html><html><head><title>Wikitechy HTML onbeforeunload Attribute</title></head><bodyonbeforeunload="return onBeforeUnload()"><ahref="http://www.wikitechy.com/">Click here to go to Wikitechy.com</a><script>functiononBeforeUnload () {
return"Write something here...";
}
</script></body></html>
Code Explanation for onbeforeunload Attribute in HTML:
The onbeforeunload attribute fires when the document is about to be unloaded and the function onBeforeUnload() is called.
href attribute defines the URL of the page which goes to that link ("http://www.wikitechy.com/")
The function onBeforeUnload() is invoked when we press F5 or click on the link.
Output for onchange Attribute in HTML:
The output shows the link to go to Wikitechy website.
Once we click the link the dialogue box is opening with the message.
If we click the Leave button, the next page is navigated to new page in same tab.
If we click the stay button, the page is stay itself.
After clicking the leave button the page moves to wikitechy.com website.
Browser Support for onbeforeunload Attribute in HTML: