BeerFizz
Technical User
- Jan 23, 2004
- 28
I'm using AJAX for the first time and while writing some code I noticed that IE appeared to be miss-behaving. Attached below is the test case showing the issue. This appears to fail with IE 6 and IE 7. Works fine with firefox and chrome.
In the code I have a number of alerts showing the path through the code and everything works ok until (with IE only) the call to the call back function setOutput. With IE the second time the doAjax script is invoked, setOutput's never called (or at least there is never a ready state of 4).
The way to reproduce this, is to delete IE's temporary internet files (tools/delete browsing history/temporary internet files). Load the test script test.html. Then click the "Click here" button and follow the path through. The first time it works fine and SetOutput is indeed called. If the button is clicked a second time, setOutput's never called. You can repeat this procedure (deleting browser history and clicking "click here").
I tried deleting the ajaxObject object to see if that would help, but it did not.
All help appreciated,
Thanks
Phil
----------------------------------------------
<code>
---------------- test.html --------------------------------------
<head>
<script language="javascript" type="text/javascript">
<!--
var ajaxObj = null;
function getAjaxObj()
{
alert("getAjaxObj: Getting ajax object.");
alert("getAjaxObj: req is " + typeof req);
// native XMLHttpRequest object
if(window.XMLHttpRequest && !(window.ActiveXObject)) {
alert("1");
try {
req = new XMLHttpRequest();
alert("1.1");
} catch(e) {
req = false;
alert("1.2");
}
// IE/Windows ActiveX version
} else if(window.ActiveXObject) {
alert("2");
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
alert("2.1");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
alert("2.2");
} catch(e) {
req = false;
alert("2.3");
}
}
} else {
alert("getAjaxObj: Your browser does not support AJAX.");
req = false;
}
alert("3");
alert("getAjaxObj: req is " + typeof req);
return req;
}
function setOutput()
{
if (ajaxObj.readyState == 4) {
if (ajaxObj.status != 200) {
alert("setOutput: Ajax Error: " + ajaxObj.status);
} else {
var cType = ajaxObj.getResponseHeader("Content-Type")
alert("setOutput: Ajax content type " + cType);
delete ajaxObj; // tried doing this to see if it would fix the issue????
}
}
}
function doAjax()
{
ajaxObj = getAjaxObj();
if (ajaxObj != null) {
ajaxObj.open("GET", ".../test.php", true);
ajaxObj.send(null);
ajaxObj.onreadystatechange = setOutput;
}
}
//-->
</script>
</head>
<body>
<button onclick="doAjax()">Click here</button>
</body>
------------------- test.php ----------------------------------------------------
<?php
echo ("Hello World");
?>
</code>
In the code I have a number of alerts showing the path through the code and everything works ok until (with IE only) the call to the call back function setOutput. With IE the second time the doAjax script is invoked, setOutput's never called (or at least there is never a ready state of 4).
The way to reproduce this, is to delete IE's temporary internet files (tools/delete browsing history/temporary internet files). Load the test script test.html. Then click the "Click here" button and follow the path through. The first time it works fine and SetOutput is indeed called. If the button is clicked a second time, setOutput's never called. You can repeat this procedure (deleting browser history and clicking "click here").
I tried deleting the ajaxObject object to see if that would help, but it did not.
All help appreciated,
Thanks
Phil
----------------------------------------------
<code>
---------------- test.html --------------------------------------
<head>
<script language="javascript" type="text/javascript">
<!--
var ajaxObj = null;
function getAjaxObj()
{
alert("getAjaxObj: Getting ajax object.");
alert("getAjaxObj: req is " + typeof req);
// native XMLHttpRequest object
if(window.XMLHttpRequest && !(window.ActiveXObject)) {
alert("1");
try {
req = new XMLHttpRequest();
alert("1.1");
} catch(e) {
req = false;
alert("1.2");
}
// IE/Windows ActiveX version
} else if(window.ActiveXObject) {
alert("2");
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
alert("2.1");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
alert("2.2");
} catch(e) {
req = false;
alert("2.3");
}
}
} else {
alert("getAjaxObj: Your browser does not support AJAX.");
req = false;
}
alert("3");
alert("getAjaxObj: req is " + typeof req);
return req;
}
function setOutput()
{
if (ajaxObj.readyState == 4) {
if (ajaxObj.status != 200) {
alert("setOutput: Ajax Error: " + ajaxObj.status);
} else {
var cType = ajaxObj.getResponseHeader("Content-Type")
alert("setOutput: Ajax content type " + cType);
delete ajaxObj; // tried doing this to see if it would fix the issue????
}
}
}
function doAjax()
{
ajaxObj = getAjaxObj();
if (ajaxObj != null) {
ajaxObj.open("GET", ".../test.php", true);
ajaxObj.send(null);
ajaxObj.onreadystatechange = setOutput;
}
}
//-->
</script>
</head>
<body>
<button onclick="doAjax()">Click here</button>
</body>
------------------- test.php ----------------------------------------------------
<?php
echo ("Hello World");
?>
</code>