Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using script i found, but it's using cache instead of new info...

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
0
0
here's my code:

Code:
function createRequestObject() {
var ro;
browser = navigator.appName;
 if(browser == "Microsoft Internet Explorer") {
 ro = new ActiveXObject("Microsoft.XMLHTTP");
 }
 else{
 ro = new XMLHttpRequest();
 }
return ro;
}

var http = createRequestObject();

function runvcodecheck() {
http.open('get', 'test-script.pl');
http.onreadystatechange = VCodeCheckResponse;
http.send(null);
}

function VCodeCheckResponse() {
 if(http.readyState == 4){
 response = http.responseText;
alert(response);
 }
}

this works great, it pulls the info from the file.

however, the info contained in the file changes, and when i run the script again, it still pulls the old info...even if i refresh the page.

i was reading up on this, and it seems that the HTTP call looks in the cache as to minimize the server calls(?).

so, any ideas to keep the content fresh, so that i pull from the file each and every time?

very new to ajax, so thanks in advance.

thanks!

- g
 
okay...

seems like if i change:

Code:
http.send(null);

to:

Code:
http.send(a=1);

it doesn't pull from cache.

is this a good fix, or does it just "happen" to work?

maybe a quick explanation as to why this is working would help. does it matter what i'm putting in the parenthesis?

thanks!

- g
 
so, any ideas to keep the content fresh, so that i pull from the file each and every time?

Yes - use server-side cache-control headers. They work perfectly, and require no client-side modifications (yes - even on IE with its out-of-the-box 'automatic' setting!)

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
To answer an earlier question by spewn, the reason that you didn't pull from the cache by placing a=1 in the send method of the http object is that you effectively changed the URL that the ajax object was going after; different URL, hence not a cached page.

The following two statements are equivalent when issuing a GET request:

Code:
http.open('get', 'myfile.php?ts=1234567890');
http.send(null);

and
Code:
http.open('get', 'myfile.php') ;
http.send('ts=1234567890') ;

The method I prefer for getting around the AJAX caching is to add the current timestamp to the end of the URL I'm requesting. This pretty much solves the caching issue.

HTH


Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top