By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,977 Members | 1,638 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,977 IT Pros & Developers. It's quick & easy.

Problem with XMLhttp

P: 2
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. var xmlhttp = null;
  4.  
  5. function SetURLDiv(url) {
  6.   if (window.XMLHttpRequest) {
  7.     // code for all new browsers
  8.     xmlhttp=new XMLHttpRequest();
  9.    }
  10.   else if (window.ActiveXObject) {
  11.     // code for IE5 and IE6
  12.    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  13.   }
  14.   if (xmlhttp!=null) {
  15.     xmlhttp.onreadystatechange=setNewDiv;
  16.     xmlhttp.open("GET",url,true);
  17.     xmlhttp.send(null);
  18.   }
  19.   else {
  20.     alert("Your browser was unable to download the required help.");
  21.   }
  22. }
  23.  
  24. function setNewDiv() {
  25.   if ((xmlhttp.readyState==4) && (xmlhttp.status == 200)) {  // 4 = "loaded"  200 = ok
  26.     // firstly remove the old help.
  27.     var unwantedDiv = document.getElementById('Insert');
  28.     unwantedDiv.parentNode.removeChild(unwantedDiv);
  29.     // Now install the new help.
  30.     document.getElementById('InsertSite').innerHTML=xmlhttp.responseText;
  31.   }
  32. }
  33.  
  34. function printURL(url) {
  35.   if (window.XMLHttpRequest) {
  36.     // code for all new browsers
  37.     xmlhttp=new XMLHttpRequest();
  38.    }
  39.   else if (window.ActiveXObject) {
  40.     // code for IE5 and IE6
  41.    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  42.   }
  43.   if (xmlhttp!=null) {
  44.     alert('One');
  45.     xmlhttp.onreadystatechange=printPage;
  46.     xmlhttp.open("GET",url,true);
  47.     xmlhttp.send(null);
  48.   }
  49.   else {
  50.     alert("Your browser was unable to download the required information.");
  51.   }
  52. }
  53.  
  54. function printPage() {
  55.   alert('Two');
  56.   alert(xmlhttp.readyState + '  ' + xmlhttp.status);
  57.   alert(xmlhttp.responseText);
  58.   if ((xmlhttp.readyState==4) && (xmlhttp.status == 200)) {  // 4 = "loaded"
  59.     alert('Three');
  60.     var printDiv = document.getElementById('print');
  61.     document.getElementById('print').innerHTML=xmlhttp.responseText;
  62.     window.print();
  63.   }
  64. }
  65.  
  66.  
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
Apr 9 '08 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
Welcome to bytes.com!

I would suggest that you use different variables for the two requests in case the two are conflicting, e.g. xmlhttp2 for the printURL and printPage.

PS. thanks for using code tags. You forgot, though, to close the tag properly by using a space in the end tag.
Apr 9 '08 #2

P: 2
Welcome to bytes.com!

I would suggest that you use different variables for the two requests in case the two are conflicting, e.g. xmlhttp2 for the printURL and printPage.

PS. thanks for using code tags. You forgot, though, to close the tag properly by using a space in the end tag.

Thanks for the reply.

I don't know why but after fooling around for another couple of hours I got someone else to try it out from our website over the internet and it worked. ?? So I went back and tried yet again from my own computer and it too worked. The code was more or less the same, and nothing likely to affect the problem. So I still don't know what went wrong.

I did add the closing tag originally but obviously it got lost in the paste and I didn't pick it up.

Cheers

Andrew
Apr 9 '08 #3

acoder
Expert Mod 15k+
P: 16,027
No problem, glad you've got it working. If you have any problems, post back to the forum and we'll see what we can do.
Apr 9 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.