Summary of methods to prevent jquery Ajax load from using caching

Source: Internet
Author: User
Tags current time php file


This article mainly introduces the method to prevent jQuery ajax Load from using cache. Friends in need can come to refer to it, I hope to help everyone

 
I. Usage
The load function of jquery is a call that requests another file and loads it into the current DOM. The complete format of the load method is: load (url, [data], [callback]) (note that no parameter is a GET request, and parameters are POST method).

* url: refers to the address of the file to be imported.
* data: optional parameters; because Load can import not only static html files, but also dynamic scripts, such as PHP files, so when importing dynamic files, we can put the parameters to be passed here.
* callback: optional parameter; refers to another function that is executed after the load method is called and the server responds.

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

jQuery Load sample code:

Copy the code code as follows:

$ (document) .ready (function () {
  $ ("# labels"). load ("/ blog / categories / labels.html");
  // When the page loads, insert the content of labels.html in the DOM element with ID #labels.
});

After I updated labels.html, the load method still uses the old labels.html in IE7, even if I press the refresh button, it doesn't work. Fortunately, jQuery provides a way to prevent Ajax from using the cache. Add the following statement to the JavaScript file in the head to solve the problem.
Copy the code code as follows:

$ .ajaxSetup ({
    cache: false // Close the corresponding cache of AJAX
});

In addition, I will introduce several methods to solve the cache. Note: I haven't tested on jQuery load, these methods are for reference only!
1. Change the file name, such as labels.html to lables_new.html, but this is no way, generally no one does this.

2. Add a specific time after labels.html, such as lables.html? 20081116. In 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">

4.load function can not only call HTML, but also script, such as labels.php, you can use the header function in the php file:

Copy the code code as follows:

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

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

Related Article

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.