onunload event



 The onunload event is exactly the opposite of onload. Onunload is an event that is triggered immediately when a user wants to leave the page, while the commonly used onload event is triggered immediately after the page is loaded. There is a big difference. Onunload is a common application such as when a user is about to close For a certain page, a window will pop up and ask if you want to close it. If you want to do a better job, you can also use AJAX to determine that the article that the netizen is writing has not been archived, and then ask whether you want to save it as a draft or archive and then close it. Wait for intimate reminders.


Basic syntax of onunload event

onunload = "javascript function to be triggered"


The "javascript function to be triggered" in the basic syntax is a required item.

Onunload event example 1. Write in the <body> tag
<body onunload="alert('See you next time!')">

Written in the HTML body tag can be said to be the most direct writing, and when users ready to close the page, it will alert the "next time" such a dialogue window, but a little reminder here about, this design is actually a bit redundant, even It may cause disgust from netizens and should be avoided as much as possible.

Onunload event example 1. Write in JavaScript area
<script type='text/javascript'>
window.onunload=SayGoodbye;
function SayGoodbye(){
 alert('bye');
}
</script>

The example uses window.onunload to execute it through the window.object method of DOM . The effect of this example is actually the same as Example 1. The only difference is that the onunload syntax is written in the area of ​​javascruipt. The advantage is that it is used to embed the entire The management of javascript syntax will be more efficient, and the webpage code can be organized cleaner. In addition, designers may need to know that not all browsers support the onunload event. Please test several browsers before using it and consider whether the target user’s browser supports it.

Post a Comment

0 Comments