1

I am initializing a download on click of a button. The problem is that for the first time the request type is 200 (loading for the first time) and when I click on the download again its taking the data from cache (304 request type) I need to make it load new data every time and disable cache. How can I achieve this??

currently I use

 $("#downloadbutton").live("click", function () {
window.location = "Handlers/somename.ashx?value="+somevalue+"&xxxxx="+$(".someid").text();

on button live click function. I am fine if we can avoid the same using ajax call(cache:false).

Vignesh Subramanian
  • 7,161
  • 14
  • 87
  • 150
  • possible duplicate: http://stackoverflow.com/questions/3002410/how-to-prevent-caching-from-jquery-ajax – Mahdi Jun 12 '13 at 05:58
  • No I dont think that will solve my issue In that case the cache data is coming even after giving giving cache false in **AJAX** call I am not doing any ajax calls here and its just 'javascript' button click which is gonna call a handler that will return some document – Vignesh Subramanian Jun 12 '13 at 06:12

2 Answers2

1

if you have access to target page, you can use following meta tags in target page to disable caching.

<head>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="pragma" content="no-cache" />    
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
</head>
Prakash GPz
  • 1,675
  • 4
  • 16
  • 27
  • But this will disable cache for the entire page which might bring down the performance as load time will increase. Can we disable cache only for that button click? can we do the page redirection in ajax-jquery? so that we can specify cache:false for the particular request – Vignesh Subramanian Jun 12 '13 at 05:56
  • 1
    Can Handlers/somename.ashx output cache control headers? See http://stackoverflow.com/questions/2970938/ideal-http-cache-control-headers-for-different-types-of-resources – Carl Jun 12 '13 at 06:02
1

Add a random number to the querystring of the file you are calling in the click event. This should trigger the browser to reload rather than use cache as you are tricking it to think it's a new page request.

Example:

 $("#downloadbutton").live("click", function () {
var timestamp = (new Date()).getTime();
window.location = "Handlers/somename.ashx?value="+somevalue+"&xxxxx="+$(".someid").text() + "ts=" + timestamp;
JanR
  • 6,052
  • 3
  • 23
  • 30