Home >Web Front-end >JS Tutorial >Use of jQuery load and unload functions

Use of jQuery load and unload functions

巴扎黑
巴扎黑Original
2017-06-29 10:39:082162browse

Example

When the user clicks the link to leave this page, a message box pops up:

$(window).unload(function(){
  alert("Goodbye!");
});

Try it yourself

Definition and usage

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 handler program to the unload event.

The unload() method only applies to window objects.

Syntax

event.unload(function)
Parameters Description
function Required. Specifies the function to run when the unload event is triggered.

jQuery load and unload functions use

load( url, [data], [callback]) loads the remote HTML file code and inserts it into the DOM. The GET method is used by default - automatically converted to POST method when passing additional parameters. In jQuery 1.2, you can specify a selector to filter the loaded HTML document, and only the filtered HTML code will be inserted into the DOM. The syntax is "url #some > selector".
has three parameters, namely:
url — (String) — URL of the HTML webpage to be loaded.
data (optional) — (Map,String) — Key/value data sent to the server. In jQuery 1.3 it is also possible to accept a string .
callback (optional) — Callback function when Callback is loaded successfully.

Example:

Load the article sidebar navigation section into an unordered list.

HTML code:
jQuery Links:

jQuery code:
$("#links ").load("/Main_Page #p-Getting-Started li");

Load the contents of the feeds.html file.

jQuery Code:
$("#feeds").load("feeds.html");

Same as above, but send additional parameters in POST form and display information on success.

jQuery code:
$("#feeds").load("feeds.php", {limit: 25}, function(){
alert("The last 25 entries in the feed have been loaded");
});

unload(fn) Bind a handler function to the unload event of each matching element.
Parameters: fn — Function The processing function bound in the unload event of each matching element.
Example: A
warning box pops up when the page is unloaded:jQuery code:
$(window).unload( function () { alert("Bye now!" ); } );

load(url, [data], [callback]) Loads the remote HTML file code and inserts it into the DOM. The GET method is used by default - automatically converted to POST method when passing additional parameters. In jQuery 1.2, you can specify a selector to filter the loaded HTML document, and only the filtered HTML code will be inserted into the DOM. The syntax is "url #some > selector". has three parameters, namely:
url — (String) — URL of the HTML webpage to be loaded.
data (optional) — (Map,String) — Key/value data sent to the server. In jQuery 1.3 it is also possible to accept a string.
callback (optional) — Callback function when Callback is loaded successfully.

Example:

Load the article sidebar navigation section into an unordered list.

HTML code:
jQuery Links:

jQuery code:
$("#links ").load("/Main_Page #p-Getting-Started li");

Load the contents of the feeds.html file.

jQuery Code:
$("#feeds").load("feeds.html");

Same as above, but send additional parameters in POST form and display information on success.

jQuery code:
$("#feeds").load("feeds.php", {limit: 25}, function(){
alert("The last 25 entries in the feed have been loaded");
});

unload(fn) Bind a handler function to the unload event of each matching element.
Parameters: fn — Function The processing function bound in the unload event of each matching element.
Example: A warning box pops up when the page is unloaded:
jQuery code:
$(window).unload( function () { alert("Bye now!"); } );

The above is the detailed content of Use of jQuery load and unload functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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