1.load: The jQuery load() method is a simple but powerful AJAX method. The load() method loads data from the server and places the returned data into the selected element.
Syntax: $(selector).load(URL,data,callback );
The required URL parameter specifies the URL you wish to load.
The optional data parameter specifies the query string set of key/value pairs sent with the request.
The optional callback parameter is the name of the function to be executed after the load() method is completed.
This is the content of the sample file ("demo_test.txt"):
<h2>jQuery and AJAX is FUN!!!</h2> <p id="p1">This is some text in a paragraph.</p>
The following example will load the content of the file "demo_test.txt" into the specified
element:
Example: $("#p1").load("demo_test.txt");
You can also add jQuery selectors to URL parameters.
The following example loads the content of the element with id="p1" in the "demo_test.txt" file into the specified
element:
$("#p1").load("demo_test.txt #p1");
Optional callback parameter Specifies the callback function to be allowed when the load() method is completed. The callback function can set different parameters:
##responseTxt - Contains the result content when the call is successful
statusTXT - Contains the status of the call
xhr - Contains the XMLHttpRequest object
unload:When the user clicks the link to leave this page, a message box# will pop up. ##: $(window).unload(function(){
alert("Goodbye!");
});
Specifically, the unload event will be emitted when the following occurs:
Usage:·object.onbeforeunload = handler ·
Triggered by: Example: <html xmlns="http://www.w3.org/1999/xhtml">
·Close the browser window
·When going to other pages through the address bar or favorites
·When clicking return, forward, refresh, one of the homepages
·When clicking on a url link to other pages
·When calling any of the following events: click, document write, document open, document close, window close, window navigate, window NavigateAndFind, location replace, location reload, form submit.
·When using window open to open a page and pass the name of the window on this page to the page to be opened.
·When reassigning the value of location.href.
·When submitting a form with a specified action through the input type="submit" button.
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>onunload测试</title>
<script>
function checkLeave(){
alert("欢迎下次再来!");
}
</script>
</head>
<body onunload="checkLeave()"></body>
</html>
4.onbeforeunload:
说明:目前三大主流浏览器中firefox和IE都支持onbeforeunload事件,opera尚未支持。
用法:
·object.onbeforeunload = handler
·
描述:
事件触发的时候弹出一个有确定和取消的对话框,确定则离开页面,取消则继续待在本页。handler可以设一个返回值作为该对话框的显示文本。
Triggered by:
·Close the browser window
·When going to other pages through the address bar or favorites
·When clicking return, forward, refresh, or one of the homepages
·When clicking a url link to another page
·Call any of the following Events: click, document write, document open, document close, window close, window navigate, window NavigateAndFind, location replace, location reload, form submit.
·When using window open to open a page , and pass the name of the window of this page to the page to be opened.
·When reassigning the value of location.href.
·When submitting a form with a specified action through the input type="submit" button.
Can be used in the following elements:
·BODY, FRAMESET, window
Platform support:
IE4+/Win, Mozilla 1.7a+, Netscape 7.2+, Firefox0.9+
Example:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>onbeforeunload测试</title>
<script>
function checkLeave(){
event.returnValue="确定离开当前页面吗?";
}
</script>
</head>
<body onbeforeunload="checkLeave()"></body>
</html>
但是onbeforeunload有个小毛病,就是页面刷新时,他还是会调用到onbeforeunload,为什么?其实刷新就相当于关闭了这个IE再重新打开的意思,因此还是会调用到onbeforeunload。
究竟怎么解决刷新不调用onbeforeunload呢?
window.onbeforeunload = function(){
var ##- window.screenLeft; var b
= n > document.documentElement.scrollWidth-20 ; # window.event.altKey) { alert("is to close rather than refresh
"); window.event.returnValue = "Close? ##"); }
# Note: The last two events in this article are compiled at: http://www.cnblogs.com/fredlau/archive/2009/06/10/1500490.html, Thanks to the original author
The above is the detailed content of jQuery之load、unload、onunload和onbeforeunload - liuyueyue. For more information, please follow other related articles on the PHP Chinese website!