Hi,
I am having a bit of a problem with using xmlhttp. The code of the javascript file is shown below used in Windows XP.
[CODE=javascript]
var xmlhttp = null;
function SetURLDiv(url) {
if (window.XMLHttp Request) {
// code for all new browsers
xmlhttp=new XMLHttpRequest( );
}
else if (window.ActiveX Object) {
// code for IE5 and IE6
xmlhttp=new ActiveXObject(" Microsoft.XMLHT TP");
}
if (xmlhttp!=null) {
xmlhttp.onready statechange=set NewDiv;
xmlhttp.open("G ET",url,true) ;
xmlhttp.send(nu ll);
}
else {
alert("Your browser was unable to download the required help.");
}
}
function setNewDiv() {
if ((xmlhttp.ready State==4) && (xmlhttp.status == 200)) { // 4 = "loaded" 200 = ok
// firstly remove the old help.
var unwantedDiv = document.getEle mentById('Inser t');
unwantedDiv.par entNode.removeC hild(unwantedDi v);
// Now install the new help.
document.getEle mentById('Inser tSite').innerHT ML=xmlhttp.resp onseText;
}
}
function printURL(url) {
if (window.XMLHttp Request) {
// code for all new browsers
xmlhttp=new XMLHttpRequest( );
}
else if (window.ActiveX Object) {
// code for IE5 and IE6
xmlhttp=new ActiveXObject(" Microsoft.XMLHT TP");
}
if (xmlhttp!=null) {
alert('One');
xmlhttp.onready statechange=pri ntPage;
xmlhttp.open("G ET",url,true) ;
xmlhttp.send(nu ll);
}
else {
alert("Your browser was unable to download the required information.");
}
}
function printPage() {
alert('Two');
alert(xmlhttp.r eadyState + ' ' + xmlhttp.status) ;
alert(xmlhttp.r esponseText);
if ((xmlhttp.ready State==4) && (xmlhttp.status == 200)) { // 4 = "loaded"
alert('Three');
var printDiv = document.getEle mentById('print ');
document.getEle mentById('print ').innerHTML=xm lhttp.responseT ext;
window.print();
}
}
[/CODE]
The function setURLDiv works fine in both Firefox and IE. Exactly as expected.
However the function printURL doesn't.
In Firefox gets through to the printPage function with the readyState increasing from 1 to 4 but the status always returns as 0. From readyState == 3 or 4 the responseText returns correctly in full. So the script never gets to alert('Three');
If I remove the status test then the whole think works fine and prints the page correctly.
In IE the script gets through to alert('One') but the function printPage is never called since alert('Two') is never displayed.
I originally simply copied, pasted (and modified the function name etc) so the different results are puzzling.
I would welcome any ideas on what is wrong because I am at a loss at the moment. Any ideas on what to test.?
Cheers
Andrew
I am having a bit of a problem with using xmlhttp. The code of the javascript file is shown below used in Windows XP.
[CODE=javascript]
var xmlhttp = null;
function SetURLDiv(url) {
if (window.XMLHttp Request) {
// code for all new browsers
xmlhttp=new XMLHttpRequest( );
}
else if (window.ActiveX Object) {
// code for IE5 and IE6
xmlhttp=new ActiveXObject(" Microsoft.XMLHT TP");
}
if (xmlhttp!=null) {
xmlhttp.onready statechange=set NewDiv;
xmlhttp.open("G ET",url,true) ;
xmlhttp.send(nu ll);
}
else {
alert("Your browser was unable to download the required help.");
}
}
function setNewDiv() {
if ((xmlhttp.ready State==4) && (xmlhttp.status == 200)) { // 4 = "loaded" 200 = ok
// firstly remove the old help.
var unwantedDiv = document.getEle mentById('Inser t');
unwantedDiv.par entNode.removeC hild(unwantedDi v);
// Now install the new help.
document.getEle mentById('Inser tSite').innerHT ML=xmlhttp.resp onseText;
}
}
function printURL(url) {
if (window.XMLHttp Request) {
// code for all new browsers
xmlhttp=new XMLHttpRequest( );
}
else if (window.ActiveX Object) {
// code for IE5 and IE6
xmlhttp=new ActiveXObject(" Microsoft.XMLHT TP");
}
if (xmlhttp!=null) {
alert('One');
xmlhttp.onready statechange=pri ntPage;
xmlhttp.open("G ET",url,true) ;
xmlhttp.send(nu ll);
}
else {
alert("Your browser was unable to download the required information.");
}
}
function printPage() {
alert('Two');
alert(xmlhttp.r eadyState + ' ' + xmlhttp.status) ;
alert(xmlhttp.r esponseText);
if ((xmlhttp.ready State==4) && (xmlhttp.status == 200)) { // 4 = "loaded"
alert('Three');
var printDiv = document.getEle mentById('print ');
document.getEle mentById('print ').innerHTML=xm lhttp.responseT ext;
window.print();
}
}
[/CODE]
The function setURLDiv works fine in both Firefox and IE. Exactly as expected.
However the function printURL doesn't.
In Firefox gets through to the printPage function with the readyState increasing from 1 to 4 but the status always returns as 0. From readyState == 3 or 4 the responseText returns correctly in full. So the script never gets to alert('Three');
If I remove the status test then the whole think works fine and prints the page correctly.
In IE the script gets through to alert('One') but the function printPage is never called since alert('Two') is never displayed.
I originally simply copied, pasted (and modified the function name etc) so the different results are puzzling.
I would welcome any ideas on what is wrong because I am at a loss at the moment. Any ideas on what to test.?
Cheers
Andrew
Comment