To prevent jquery Ajax load using caching method summary _jquery

Source: Internet
Author: User

One, usage
jquery's load function is a call that requests another file and loads it into the current DOM, and the full format of the Load method is: Load (URL, [data], [callback]) (note that no parameters are get-mode requests, and parameters are post methods).

* URL: refers to the address of the file to be imported.
* Data: optional parameters; Because load can not only import static HTML files, but also import dynamic scripts, such as PHP files, so to import a dynamic file, we can put the parameters to pass here.
* Callback: optional parameter; is another function that executes after invoking the Load method and getting a response from the server.

Caching this thing, to some extent, speeds up the loading of the page, but it often brings us trouble. I briefly introduced the use of the Load method in jquery in the last article. In practical use, we may encounter the problem of browser caching. For example, I met this problem in IE7.

JQuery Load Sample code:

Copy Code code as follows:

$ (document). Ready (function () {
$ ("#labels"). Load ("/blog/categories/labels.html");
Inserts the contents of the labels.html in the DOM element with ID #labels when the page is loaded.
});

When I update labels.html, the Load method in IE7 is still using the old labels.html, even if I press the Refresh key, it doesn't work. Fortunately jquery provides a way to prevent Ajax from using caching, and adding the following statement to the head's JavaScript file can solve the problem.
Copy Code code as follows:

$.ajaxsetup ({
Cache:false//Turn off AJAX-appropriate caching
});

In addition, I'll introduce several ways to fix caching. Note: I have not tested the jquery load problem, these methods are for reference only!

1. Change the filename, such as labels.html to lables_new.html, but this is no way, generally no one does so.

2. Add specific time after labels.html, such as lables.html?20081116. In the actual work, after I update the Css/javascript file, I use this method to prevent the file from being cached.

3. Add the following statement at the top of the labels.html file:

<meta http-equiv= "Pragma" content= "No-cache" >
<meta http-equiv= "Expires" content= "-1" >

The 4.load function can invoke not only HTML, but also script, such as labels.php, to use the header function in PHP files:

Copy Code code as follows:

<?php
Header ("Cache-control:no-cache, must-revalidate");
?>

Two other solutions:
Add a time parameter value in the request path to the current time or add a hidden field to the form to set the value of the field to the current time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.