I wanted to delete this thread and start over but it looks as if I can't. I've noticed that this "bug" only happens when XMLHttpRequest is used, not just when a cookie is set from a function. It also happens with div data. So to make it simpler, here is a new clear and concise example of what I am doing:
Code:
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Grrrrrrrrrrr....</title>
<style> #dataDiv { height: 100px; width: 100px; background-color: red; } </style>
</head> <body>
<div id="dataDiv">blah</div>
<script type="text/javascript"> alert ("Please make note of the DIV box contents - it shows blah. Click OK to run getUserInfo routine."); getUserInfo ("www.google.com"); alert ("getUserInfo routine has run and returned. This is the first alert line and shows the old data even though the routine that updated it has already run and returned.\n\ndataDiv innerHTML contents: " + document.getElementById("dataDiv").innerHTML); alert ("This is the very next line of code, the second alert line - and it will now show new contents.\n\ndataDiv innerHTML contents: " + document.getElementById("dataDiv").innerHTML); alert ("why???");
// The following code are functions code. // JavaScript Document
function getUserInfo(url) {
if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else { if (window.ActiveXObject) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } } if (xhr) { xhr.onreadystatechange = checkGetResult; xhr.open("GET", url, true); xhr.send(null); } else { alert ("couldnt make XMLHttp Request."); } }
function checkGetResult() { if (xhr.readyState == 4 ) { if (xhr.status == 200 ) { var outMsg = xhr.responseText; } else { var outMsg = "There was a problem. Error Code = " + xhr.status; } // What would actually go here is information from the XMLHttpRequest (outMsg) // but for the sake of simplicity in exhibiting the problem I am having, I am simply // changing the word in the DIV.
document.getElementById("dataDiv").innerHTML="newblah"; } }
</script>
</body> </html>
Run this and you will see what I mean. After the function is called which updates the DIV contents I run an alert line to display the contents of the DIV. It shows the initial contents. However the very next line displays the same exact data and it comes up with the updated info.
If I rewrite the function to only update the DIV then the update is immediate and both alert lines show the new info, so I am assuming the problem has to do with the existence of the XMLHttpRequest lines being present. Why is this?? And is there any way to work around it?
Last edited by catatung; 06-15-2010 at 02:30 AM..
|