Summary on how to prevent jQuery ajax Load from Using Cache

Source: Internet
Author: User

I. Usage
Jquery's load function is a call to request another file and load it into the current DOM. The complete load method format is: load (url, [data], [callback]). (Note that no parameter is a GET request, and a parameter is the POST method ).

* Url:The address of the file to be imported.
* Data:Optional parameter. Because Load can not only import static html files, but also dynamic scripts, such as PHP files, when importing dynamic files, we can put the parameters to be passed here.
* Callback:Optional parameter. It is another function that is executed after the load method is called and the server response is obtained.

Caching speeds up page loading to some extent, but it often brings us trouble. In the previous article, I briefly introduced the use of the Load method in jQuery. In practice, we may encounter browser cache problems. For example, I encountered this problem in IE7.

JQuery Load sample code:
Copy codeThe Code is as follows:
$ (Document). ready (function (){
$ ("# Labels"). load ("/blog/categories/labels.html ");
// Insert the content of labels.html in the domelement with the idlabelswhen the page is mounted.
});

When I updated labels.html, I used the old labels.html file in ie7, even if I pressed the refresh key. Fortunately, jQuery provides a method to prevent ajax from using the cache. You can solve the problem by adding the following statements to the javascript file of the head.
Copy codeThe Code is as follows:
$. AjaxSetup ({
Cache: false // disable the corresponding AJAX cache
});

In addition, I will introduce several methods to solve the cache. Note: I have not tested jQuery load. These methods are for reference only!

1. Change the file name, such as labels.htmlto lables_new.html. But there is no way to do this. Generally, no one does.

2.add a specific time after labels.html, such as lables.html? 20081116. In practice, after I update css/javascript files, I use this method to prevent files from being cached.

3.add the following statement to the top of the labels.html file:

<META HTTP-EQUIV = "Pragma" CONTENT = "no-cache">
<META HTTP-EQUIV = "Expires" CONTENT = "-1">

4. The load function can call not only HTML, but also script, such as labels. php. You can use the header function in the php file:
Copy codeThe Code is 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 in the form to set the value of this 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.