Problem with XMLhttp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Andrewh
    New Member
    • Apr 2008
    • 2

    Problem with XMLhttp

    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
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #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.

    Comment

    • Andrewh
      New Member
      • Apr 2008
      • 2

      #3
      Originally posted by acoder
      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

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        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.

        Comment

        Working...