I am having a bit of a problem with using xmlhttp. The code of the javascript file is shown below used in Windows XP.
Expand|Select|Wrap|Line Numbers
- var xmlhttp = null;
- function SetURLDiv(url) {
- if (window.XMLHttpRequest) {
- // code for all new browsers
- xmlhttp=new XMLHttpRequest();
- }
- else if (window.ActiveXObject) {
- // code for IE5 and IE6
- xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
- }
- if (xmlhttp!=null) {
- xmlhttp.onreadystatechange=setNewDiv;
- xmlhttp.open("GET",url,true);
- xmlhttp.send(null);
- }
- else {
- alert("Your browser was unable to download the required help.");
- }
- }
- function setNewDiv() {
- if ((xmlhttp.readyState==4) && (xmlhttp.status == 200)) { // 4 = "loaded" 200 = ok
- // firstly remove the old help.
- var unwantedDiv = document.getElementById('Insert');
- unwantedDiv.parentNode.removeChild(unwantedDiv);
- // Now install the new help.
- document.getElementById('InsertSite').innerHTML=xmlhttp.responseText;
- }
- }
- function printURL(url) {
- if (window.XMLHttpRequest) {
- // code for all new browsers
- xmlhttp=new XMLHttpRequest();
- }
- else if (window.ActiveXObject) {
- // code for IE5 and IE6
- xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
- }
- if (xmlhttp!=null) {
- alert('One');
- xmlhttp.onreadystatechange=printPage;
- xmlhttp.open("GET",url,true);
- xmlhttp.send(null);
- }
- else {
- alert("Your browser was unable to download the required information.");
- }
- }
- function printPage() {
- alert('Two');
- alert(xmlhttp.readyState + ' ' + xmlhttp.status);
- alert(xmlhttp.responseText);
- if ((xmlhttp.readyState==4) && (xmlhttp.status == 200)) { // 4 = "loaded"
- alert('Three');
- var printDiv = document.getElementById('print');
- document.getElementById('print').innerHTML=xmlhttp.responseText;
- window.print();
- }
- }
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