返回值:jQueryunload(handler(eventObject))
Bind an event handler to the "unload" JavaScript event.
-
1.0 新增unload(handler(eventObject))
handler(eventObject) (Function) A function to execute when the event is triggered. -
1.4.3 新增unload([eventData], handler(eventObject))
eventData (Object) 可选参数,将要传递给事件处理函数的数据映射。handler(eventObject) (Function) 每当事件触发时执行的函数。
This method is a shortcut for .bind('unload', handler)
.
The unload
event is sent to the window
element when the user navigates away from the page. This could mean one of many things. The user could have clicked on a
link to leave the page, or typed in a new URL in the address bar. The forward and back buttons will trigger the event. Closing
the browser window will cause the event to be triggered. Even a page reload will first create an unload
event.
The exact handling of the
unload
event has varied from version to version of browsers. For example, some versions of Firefox trigger the event when a link is followed, but not when the window is closed. In practical usage, behavior should be tested on all supported browsers, and contrasted with the proprietarybeforeunload
event.
Any unload
event handler should be bound to the window
object:
$(window).unload(function() { alert('Handler for .unload() called.'); });
After this code executes, the alert will be displayed whenever the browser leaves the current page.
It is not possible to cancel the unload
event with .preventDefault()
. This event is available so that scripts can perform cleanup when the user leaves the page.
示例:
To display an alert when a page is unloaded:
jQuery 代码:
$(window).unload( function () { alert("Bye now!"); } );