PDA

View Full Version : Browser Caching Problem



stephenalistoun
01-02-2009, 01:44 PM
Hi all,

This code works fine in firefox but not in internet explorer.


Thanks Stephen

The Code is stated below:


<script type="text/javascript">

function checkEnter(e)
{
var keynum;
var keychar;
var numcheck;

if(window.event) // IE
{
keynum = e.keyCode;
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which;
}

if(keynum == 13)
{
insertData();
}

}

function ajaxRequest()
{
var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]
//activeX versions to check for in IE

if (window.ActiveXObject)
{
//Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
for (var i=0; i<activexmodes.length; i++)
{
try
{
return new ActiveXObject(activexmodes[i])
}
catch(e)
{
alert("Failed");
}
}
}
else if (window.XMLHttpRequest)
{
return new XMLHttpRequest()
}
else
{
return false
}
return;
}


function loadURL(url)
{
//to prevent browser from caching data
var bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();

mygetrequest=new this.ajaxRequest()
mygetrequest.onreadystatechange=function()
{
if (mygetrequest.readyState==4)
{
if (mygetrequest.status == 200)
{
processData(mygetrequest.responseText); //download successful


}
else
{
alert('download failed.');
}
}
}

mygetrequest.open("GET", url+bustcacheparameter, true);
mygetrequest.send(null);
}

function processData(responseText)
{
var countValue = responseText;

if(countValue > 0)
{
showChatData("getcd.php");
}
}

function init()
{
loadURL("checkUpdate.php");

//Refresh the Screen every 1 second
setTimeout("init()",1000);
}


function showChatData(url)
{
var bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();

mygetrequest=new this.ajaxRequest()
mygetrequest.onreadystatechange=function()
{
if (mygetrequest.readyState==4)
{
if (mygetrequest.status == 200)
{
showData(mygetrequest.responseText); //download successful
}
else
{
alert('download failed.');
}
}
}

mygetrequest.open("GET", url+bustcacheparameter, true);
mygetrequest.send(null);
}



function showData(responseText)
{
document.getElementById("txtHint").innerHTML=responseText;

var objDiv = document.getElementById("txtHint");

objDiv.scrollTop = objDiv.scrollHeight;
}


function insertData()
{
str = document.getElementById("chatText").value;

font = document.getElementById("font").value;

var url = "insertRow.php?value=" + str +"&font=" + font;

var bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();

mygetrequest=new this.ajaxRequest()

mygetrequest.onreadystatechange=function()
{
if (mygetrequest.readyState==4)
{
if (mygetrequest.status == 200)
{
init();

alert(mygetrequest.status);
}
else
{
alert("status is " + mygetrequest.status);
}
}
}
mygetrequest.open("GET", url, true);
mygetrequest.send(null);

document.getElementById("chatText").value = "";
}


function smiley(str)
{
var temp = document.getElementById("chatText").value;

document.getElementById("chatText").value = temp + str;

delayhidemenu();
}
</script>

jscheuer1
01-02-2009, 04:55 PM
There could also be other problems, but unless this function:



function checkEnter(e)
{
var keynum;
var keychar;
var numcheck;

if(window.event) // IE
{
keynum = e.keyCode;
}
else if(e.which) // Netscape/Firefox/Opera
{
keynum = e.which;
}

if(keynum == 13)
{
insertData();
}

}

is called something like so in the markup:


<textarea onkeydown="checkEnter(event);">

IE will never get the event, so won't know which key was pressed. This would be a better way:


function checkEnter(e)
{
var keynum;

e = e || window.event;
keynum = e.keyCode || e.which;

if(keynum == 13)
{
insertData();
}

}

It will work even if the call is like:


document.onkeydown = checkEnter;

or attached/added using addEventListener/attachEvent.

It's hard to pick out anything really off about the rest of the code. I'll have another look, but it would help to have a link to the page.

jscheuer1
01-02-2009, 05:02 PM
I just saw something else, just as a matter of logic, in this function:


function insertData()
{
str = document.getElementById("chatText").value;

font = document.getElementById("font").value;

var url = "insertRow.php?value=" + str +"&font=" + font;

var bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();

mygetrequest=new this.ajaxRequest()

mygetrequest.onreadystatechange=function()
{
if (mygetrequest.readyState==4)
{
if (mygetrequest.status == 200)
{
init();

alert(mygetrequest.status);
}
else
{
alert("status is " + mygetrequest.status);
}
}
}
mygetrequest.open("GET", url + bustcacheparameter, true);
mygetrequest.send(null);

document.getElementById("chatText").value = "";
}

You should add the highlighted as shown.