PDA

View Full Version : Ajaxpage fails randomly



nielsterp
08-07-2011, 09:44 AM
1) Script Title: Ajaxcontent

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm

3) Describe problem: I'm using this script to populate a homepage divided into DIV's. I have a left DIV wich hold the menu, and is static. Then I have a headline DIV and a main contents DIV.

When I select a new page on my menu, first the appropriate headline is loaded, then the iframe holding the contents.

Much of the time everything works fine, but SOMETIMES the headline fails to load completely. At a few occations you can see the headline for a fraction of a second, then it disapears again.

The only cure I have found is to click my menu item several times, until the whole thing have loaded. Probably not something that will bring me a price for good webdesign :-).

Can anybody help ? Below is the javascript code that actualy calls the script:

[CODE]
// JavaScript Document


/***********************************************
* Dynamic Ajax Content- Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}

function forside()
{
javascript:ajaxpage('index.php','fullpage');
javascript:ajaxpage('forsideoverskrift.php','overskrift');
javascript:ajaxpage('forside.php','mainContent');
}

function alkohol()
{
javascript:ajaxpage('alkoholoverskrift.php','overskrift');
javascript:ajaxpage('alkohol.php','mainContent');
}

function astrofysik()
{
javascript:ajaxpage('astrofysikoverskrift.php','overskrift');
javascript:ajaxpage('astrofysik.php','mainContent');
}

function admin()
{ javascript:ajaxpage('Administration/index.php','fullpage')
javascript:ajaxpage('Administration/adminoverskrift.php','overskrift');
javascript:ajaxpage('Administration/adminforside.php','mainContent');
}

function phpMyAdmin()
{
javascript:ajaxpage('Administration/index.php','fullpage')
javascript:ajaxpage('Administration/forsidephpmyadmin.php','overskrift');
javascript:ajaxpage('Administration/iframe_phpMyAdmin.php','mainContent');
}

function sqlbuddy()
{
javascript:ajaxpage('Administration/forsidesqlbuddy.php','overskrift');
javascript:ajaxpage('Administration/iframe_sqlbuddy.php','mainContent');
}

function phpinfo()
{
javascript:ajaxpage('Administration/forsidephpinfo.php','overskrift');
javascript:ajaxpage('Administration/iframe_phpinfo.php','mainContent');
}

function webmin()
{
javascript:ajaxpage('Administration/forsidewebmin.php','overskrift');
javascript:ajaxpage('Administration/iframe_webmin.php','mainContent');
}

function statistik()
{
javascript:ajaxpage('Administration/forsidestatistik.php','overskrift');
javascript:ajaxpage('Administration/iframe_webalizer','mainContent');
}

jscheuer1
08-07-2011, 03:14 PM
Looking at what you have there, I'd suspect the problem is that some of those external pages are pretty intensive on the server side. The server may become overloaded and the request may expire before before the external page is assembled.

I don't know enough about servers and PHP to know for sure, but I imagine that length of time is set somewhere, perhaps in more than one place. And it probably returns a numeric value like 404 for not found or 500 for forbidden. If you can find out what number that is, you could trap that and try again and/or extend the timeout period for requests on the server, that might fix it.