Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 4.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-17-2008, 02:45 AM   PM User | #1
ballboy
New to the CF scene

 
Join Date: Jun 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ballboy is an unknown quantity at this point
Ajax IE caching nightmare

Hi,

I'm having trouble with an Ajax script that auto refreshes content on a page. The code worked fine on FireFox, and then i found out that it wont work on either IE6 or 7. I tested further on Opera (didn't work) and Safari (did work).

I've been doing some reading about this issue and found that apart from caching via headers some people pass a random number as a variable in the url which is supposed to solve the problem. surprisingly (or not) it fixed the problem on Opera, but not on either version of IE.

The script just loads a time stamp from a file called time.php

Im attaching the code in hope someone might put out me of my misery

Code:
<?php
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>

<head>
<script type="text/javascript">
var randomnumber = Math.floor(Math.random()*1000001);
var page = "/4xp/ajax/time.php?dummy=" + randomnumber;

function ajax(url,target)
 {
 
    // native XMLHttpRequest object
   document.getElementById(target).innerHTML = 'sending...';
   if (window.XMLHttpRequest) {
       req = new XMLHttpRequest();
       req.onreadystatechange = function() {ajaxDone(target);};
       req.open("GET", url, true);
       req.send(null);
   // IE/Windows ActiveX version
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
       if (req) {
           req.onreadystatechange = function() {ajaxDone(target);};
           req.open("GET", url, true);
           req.send(null);
       }
   }
		   setTimeout("ajax(page,'scriptoutput')", 1000);
}

function ajaxDone(target) {
// only if req is "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200 || req.status == 304) {
results = req.responseText;
document.getElementById(target).innerHTML = results;
} else {
document.getElementById(target).innerHTML="ajax error:\n" +
req.statusText;
}
}
}
</script>
</head>
<body onload="ajax(page,'scriptoutput')">
<p>Current Server date & time:<br />
<span id="scriptoutput"></span></p>
</body>
and time.php is just:

Code:
<?php
echo time();
?>
Thanks!
ballboy is offline   Reply With Quote
Old 06-17-2008, 03:53 AM   PM User | #2
bdl
Regular Coder

 
Join Date: Apr 2007
Location: Camarillo, CA US
Posts: 590
Thanks: 4
Thanked 83 Times in 82 Posts
bdl is an unknown quantity at this point
I used to use the random GET string hack, but now I've found setting a header with the XMLHttpRequest setRequestHeader() method is best:
Code:
/* xhr is your request object of course */
xhr.open("GET","script.php",true);
/* send If-Modified-Since header
xhr.setRequestHeader("If-Modified-Since", "Fri, 31 Dec 1999 23:59:59 GMT");
xhr.onreadystatechange= function() {
 /* callback function */
}
xhr.send(null);
You shouldn't have to worry about any caching issues, the header ensures it always returns a result based on your If-Modified-Since header. Just make sure it's long enough in the past to work. I use "doomsday eve", AKA the night before Y2K.
bdl is offline   Reply With Quote
The Following 2 Users Say Thank You to bdl For This Useful Post:
Eris (07-26-2008), oesxyl (06-17-2008)
Old 07-26-2008, 04:41 PM   PM User | #3
Eris
New to the CF scene

 
Join Date: Jul 2008
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Eris is an unknown quantity at this point
Thumbs up

I've searched so long how to solve this problem and finally found the way. This really works. Thx A LOT bdl.
Eris is offline   Reply With Quote
Old 09-03-2008, 08:48 AM   PM User | #4
babelfish
Regular Coder

 
Join Date: Jun 2002
Location: England =)
Posts: 518
Thanks: 25
Thanked 0 Times in 0 Posts
babelfish can only hope to improve
yes, fixed my problem too! kudos!
__________________
"They hired me for my motivational skills. Everyone at work says they have to work much harder when I`m around" Homer J Simpson
babelfish is offline   Reply With Quote
Old 09-25-2008, 07:59 PM   PM User | #5
ctess
New to the CF scene

 
Join Date: Sep 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
ctess is an unknown quantity at this point
There is a much easier option and you can remove all the caching headers at the top as well. This solved all my caching issues:

xmlHttp.open('GET', url+'?bustcache='+new Date().getTime(), true);
ctess is offline   Reply With Quote
Old 10-15-2008, 12:48 AM   PM User | #6
xcaliburs
New to the CF scene

 
Join Date: Oct 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
xcaliburs is an unknown quantity at this point
Thumbs up

To bdl,

Thank you for posting! You're the man

Cheers
xcaliburs is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:11 PM.


Advertisement
Log in to turn off these ads.