JavaScript onbeforeunload EventThe onbeforeunload Event happens just before a document is unloaded. It displays a statement in a confirmation dialogue box to stay current page or go to the next page using the provided link. You may ask the user whether they wish to stay on the current page with the help of the onbeforeunload Event. In other words, Any link that will take you to a new page, such as a text link, an image link, or another type of link, triggers the onbeforeunload Event. If users click on ok of the confirmation box, go to the next page and click on cancel, then keep on the same page. It is impossible to delete the dialogue box's message. SyntaxThe onbeforeunload Event uses multiple tags and functions.
Explanation When you are going to leave a webpage that has a beforeunload event listener, the beforeunload Event will cause a confirmation box to appear asking you if you are sure you want to leave. If you proceed, the browser directs you to the new page. Otherwise, the navigation is stopped. As per the specification, the confirmation dialogue must be displayed by invoking the preventDefault() method within the beforeunload event handler.
Explanation In the past, several browsers let you show a unique message on the confirmation dialogue. The users were supposed to be warned that their data would be lost if they navigated away. Unfortunately, user fraud is the common usage for this capability. A custom response is no longer accepted as a result. According to the Javascript specification, the beforeunload event handler ignores the methods to confirm(), alert(), and prompt() functions. ExamplesThe following examples use the onbeforeunload property for the link. Example 1 The onbeforeunload property uses the windows and gets the confirmation box. Here, we use basic Events with HTML tags. Output The below image shows the onbeforeunload Event with the confirmed values. Output1 First time click on the link Output2 Second time click on the link Example 2 The onbeforeunload property uses the windows and gets the confirmation box. Here, we use basic Events with the data. Output The below image shows the onbeforeunload Event with the confirmed values. Example 3 The onbeforeunload property uses the windows and gets the confirmation box. Here, we use basic eveni inside of the windows.addEventListener property with the function to get the link. Output The below image shows the onbeforeunload Event with the confirmed values. Example 4 The onbeforeunload property uses without windows and gets the confirmation box. Here, we use a basic Event inside the addEventListener property for the web page. Output The below image shows the onbeforeunload Event without windows. Example 5 The onbeforeunload property uses without the windows and gets the confirmation box. Here, we use Event for the web page with the data. Output The below image shows the onbeforeunload Event without windows. Web Browsers SupportedThe following browsers and its version supported the onbeforeunload Event.
ConclusionThe javascript onbeforeunload Event works for the load link or page with the confirmation. It loads a web page or windows with the event object. It helps before loading the web page and getting the proper next page. Next TopicRead Data from JavaScript IndexedDB |