Jump to content

Refresh


jaymc

Recommended Posts

I am trying to have AJAX fetch the contents of a page and load it into a div

 

Its working fine, however, I need it to do it very 30 seconds which again ive got working fine

 

The problem is, it seems that after 30 seconds, when the page is requested its not actually giving the live contents of that page as the page produces dynamic data

 

Im assuming after the first initial request made by AJAX the page contents are cached therefor if requested again you will get the cached data not the live set

 

Here is the code I have

 

function open_url(url, target) {

	if (window.ActiveXObject) {
  		link = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
  		link = new XMLHttpRequest();
	}


	link.onreadystatechange = function() { response(url, target); }
	link.open("GET", url, true);
	link.send(null);
}

function response(url, target) {
	if (link.readyState == 4) {
 	document.getElementById(target).innerHTML = (link.status == 200) ? link.responseText : "Ooops!! A broken link! Please contact the webmaster of this website ASAP and give him the fallowing errorcode: " + link.status;
}
}

 

Am i right in what im saying, if so is there a way to flush cached data?

 

Feedback would be great!

Link to comment
Share on other sites

Its a php file

 

Its nothing to do with the php file or the header. the ajax is caching the request

 

I tested it using php's microtime(); after the first click it does not update to the current time

 

Its definately caching, any way around it?

Link to comment
Share on other sites

  • 3 weeks later...

what you have to do is use javascripts random function before every ajax call and supply that as an extra url parameter so that the url is actually different every time. 

http://www.w3schools.com/jsref/jsref_random.asp

function open_url(url, target) {
    url = url + '&myrand=' + Math.random(); //assuming some other parameters are already being passed

Link to comment
Share on other sites

Yeh thats the way!

 

Just wondering, when it updates the divs INNERHTML, what happens to its old innerHTML, if its cached in the browser, after x amount of time you could have 5000 X innerHTML divs content wise

 

Is there a way to 'clear the buffer' as it where

Link to comment
Share on other sites

when you do an innerHTML, the old innerHTML contents are overwritten. I could concatenate innerHTML onto whats currently in there like this:

element.innerHTML = element.innerHTML + newcontent; //new contents  added to the end of my element

 

I'm not aware of any way to clear the buffer.  There are headers that can be sent back from the server page(like php) that specify to the browser that no buffering be done.  All browsers may not respect that though. 

 

If you submit your ajax fields 'POST' instead of 'GET' (assuming you have some actual fields) then the page will usually not be cached even if the data in the submitted fields are identical because browsers don't cache 'POST' submissions.

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.