Jump to content

How to force new content on page refresh?


edg322

Recommended Posts

My problem has to do with AJAX/js and innerHTML in IE.  I have a very basic AJAX script that calls a file which should display fresh content every few seconds.  All this is within div tags on my main page.

 

It works fine in FF of course.  It also works in IE if you have Tools-> Internet options -> Settings "Check for newer versions of stored pages" set to "Every visit".  If you change that to "Automatically" then it breaks.  Actually if you change it to anything other than the "Every visit" it doesn't work.

 

Apparently Microsoft caches in the temp files every GET request.  Well guess what, I change the AJAX call to POST and it's the same problem.

 

So the POST option is out. 

 

I also can't ask users to change their browser settings. 

 

I also can't change my meta tags, which I believe has an option somewhere to force a new page.

 

So I'm wondering if there is a way in javascript to force new contents on every page refresh.  Anyone know a way to do it like that?

Link to comment
Share on other sites

Here's the code:

Code:

 

function GetXmlHttpObject() {
var xmlHttp=null;
try {xmlHttp=new XMLHttpRequest();} catch(e) {
	try {xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");} 
                        catch(e) {xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
}
return xmlHttp;
}

function getPage(thePageToGet) {
xmlHttp=GetXmlHttpObject();
if(xmlHttp==null) {alert("Your browser doesn't support AJAX!");return;}

var url = thePageToGet;

xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged() {
if(xmlHttp.readyState==4) {
	if (xmlHttp.responseText) {
	  document.getElementById("featured_area").innerHTML=xmlHttp.responseText;
                  t=setTimeout('getPage("get_images.cfm")',5000);
	}
}
}

 

 

like I said, very basic script.  There's a span tag on my page called "featured_area" and that content should be refreshed from the contents of 'thePageToGet' which is just dynamically grabbing some images and the urls they link to:

 

Code:

 

<span id="featured_area">
<script>
            getPage("get_images.cfm")
</script>
</span>

Link to comment
Share on other sites

ok I got a solution worked out.  If you add the current time at the end of the URL string being called, that will force IE to get the fresh page (instead of grabbing cached content)

 

Change the getPage function to

getPage("get_images.cfm?ts=")

 

And add into the js:

 

	var now = new Date();
var url = thePageToGet + now.getTime();

 

This way instead of IE looking to refresh just "get_images.cfm" it's now looking to refresh "get_images.cfm?ts=1182352248509" which will of course change every time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.