Home > Web Front-end > JS Tutorial > jQuery之load、unload、onunload和onbeforeunload - liuyueyue

jQuery之load、unload、onunload和onbeforeunload - liuyueyue

巴扎黑
Release: 2017-06-29 10:38:11
Original
4291 people have browsed it

 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>
Copy after login

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");
Copy after login

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

The following example will be loaded () A prompt box is displayed after the method is completed. If the load() method has succeeded, "External content loaded successfully!" is displayed, and if it fails, an error message is displayed:

 $("button").click(function(){

  $("#p1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){

    if(statusTxt=="success")

    alert( "External content loaded successfully!");

if(statusTxt=="error")

alert("Error: "+xhr.status+": "+xhr.statusText);

  });

 });   

 2.

unload:When the user clicks the link to leave this page, a message box# will pop up. ##:

When the user leaves the page, the unload event occurs.

Specifically, the unload event will be emitted when the following occurs:

    Click a link to leave the page
  • New URL typed in the address bar
  • Use the forward or back buttons
  • Close the browser
  • Reload the page
  • The unload() method binds the
event handling

program to the unload event.

The unload() method only applies to window objects.

 

3.onunload

 Usage:·object.onbeforeunload = handler   ·

Description: The onunload event is triggered when the user closes a page.

Triggered by:
 ·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.

Example:

  <html xmlns="http://www.w3.org/1999/xhtml">
  <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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template