0

I'm fairly new to the world of web development and am trying to read a txt file in internet explorer 8 and compare it to source code of a website to see if they are equal. This is so I can work out if the web page is functioning correctly.

I managed to get the source code with an xmlhttprequest and have tried the same to get the text file (which is in the same domain as my web page) and I am getting an access denied error.

After some research I can see that cross-domain xmlhttprequests won't work but that's not what I'm trying to do so I'm not sure how to proceed.

Having run the same code in Firefox(current version). It will read the file but not the web page!

I don't mind which of the two browsers I end up using but at the moment each does half of what I want it to.

my code is:

function source1(){
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET", "http://website",true);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) {
            document.getElementById('textzone').value = xmlhttp.responseText
            var inputString = xmlhttp.responseText;
            alert(inputString);
            comparison(inputString)
        }
    }
    xmlhttp.send(null)
}

function comparison(inputString){
    xmlhttp1=new XMLHttpRequest();
    xmlhttp1.open("GET", "comparisondoc.txt", false);
    xmlhttp1.onreadystatechange=function() {
        if (xmlhttp1.readyState==4) {
            var compareString = xmlhttp1.responseText;
            alert(compareString)
            if(inputString==compareString){
                alert("Strings are equal");
            }
        }
    }
    xmlhttp.send(null)
}

All I need to know is why either the file won't open in ie8, or why the website source code shows up blank (in the alert) in firefox. Any help would be appreciated.

  • 1
    Did you try the three lines of code this would require with jQuery to make sure there are'nt cross browser issues with XMLHttpRequest (and there are cross browser issues with this) ? – adeneo Dec 07 '12 at 11:10
  • i don't know the first thing about jQuery unfortunatly, however If I need to learn it to make my function work I will go and learn it. – Paul Johnson Dec 07 '12 at 11:13
  • 1
    Older IE versions use active X etc. and there are differences in lots of stuff. Look at this [**SO Answer**](http://stackoverflow.com/questions/2557247/easiest-way-to-retrieve-cross-browser-xmlhttprequest) for a more "cross browser" XMLHttpRequest. – adeneo Dec 07 '12 at 11:17

3 Answers3

0

It could be a browser support issue. Try the following code to initialize your XMLHttpRequest :

function createRequest() {
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }

  if (!request)
    alert("Error initializing XMLHttpRequest!");
}
sm_
  • 2,572
  • 2
  • 17
  • 34
  • I tried this and unfortunatly ie8 still gives an access denied error and firefox still gives out blank for the website source code – Paul Johnson Dec 07 '12 at 11:37
  • Oh i am sorry, i didn't really notice that it is giving you an access denied issue. It is probably an authorization issue, could you please provide me with a wider scope of where you are trying to implement this, it could be the page implements some authorization technique, what technology are you using in the pages, are they only HTML, ASP.NET (.aspx)? are you using MVC mayb? Note: If you are using ASP.NET for example the authorization check may not be obvious, it could be in a base page that your page is inheriting.if using MVC you need to check the HTTP module implementation (URI routing) – sm_ Dec 07 '12 at 11:55
0

Check your comparison function. You should you xmlhttp1 instead of xmlhttp at 2 places

function comparison(inputString){
    xmlhttp1=new XMLHttpRequest();
    xmlhttp1.open("GET", "comparisondoc.txt", false);
    xmlhttp1.onreadystatechange=function() {
        if (xmlhttp1.readyState==4) {
            <!--alert(xmlhttp1.responseText)-->
            var compareString = xmlhttp1.responseText;
            alert(compareString)
            if(inputString==compareString){
                alert("Strings are equal");
            }
        }
    }
    xmlhttp1.send(null)
}
Akhil Sekharan
  • 12,467
  • 7
  • 40
  • 57
0

Try to add the if(xmlhttp.status == 200) { } stuff. Remember both of these are looping through status' "AND" readystates.

Technically you could be erroring somewhere (I'd rather not speculate on) halting progress to next request or whatever without the status check.

Also you "should" try other request techniques. ie.. xmlhttp.onreadystatechange = function(){itsReady(inputString)}; // we keep this line short and simple calling to another func that contains your status and readystate checks, response stuff, and more func.

On a pretty normal run the Loop looks like: hi rdySte:1///status 0//////// hi rdySte:2///status 200//////// hi rdySte:3///status 200//////// hi rdySte:4///status 200////////

I ran into a lot of weird issues trying the long onreadystatechange = function (){ ... All stuff..} I successfully run a crazy set of request functionalities using the short onreadystatechange technique.

I noticed at the last minute-> is there a reason why the async flags are different between your funcs? I'd set them all to true unless you have a great reason.

This will work: (to test: 2 pages t1.php contains a num or whatever and t2.txt that has a num in sam dir as the funcs are called in)

function source1(){
                                            var avar = 1;
                                                xmlhttp=new XMLHttpRequest();
                                                xmlhttp.open("GET", "t1.php",true); // shortened f-names for ease of test
                                                xmlhttp.onreadystatechange = function(){jsg_snd(avar)};
                                                xmlhttp.send(null)
}

function jsg_snd(avar){
    if (xmlhttp.readyState==4) {
        if (xmlhttp.status == 200) {
                                            var inputString = xmlhttp.responseText;
                                                document.getElementById('text_zone').innerHTML = inputString;
                                                document.getElementById('text_zone1').value = inputString;
                                                // alert(inputString);//
                                                comparison(inputString)
        }
    }
}

function comparison(inputString){
        xmlhttp1=new XMLHttpRequest();
                                                xmlhttp1.open("GET", "t2.txt", true);
                                                xmlhttp1.onreadystatechange= function(){jsg_snd1(inputString);};
                                                xmlhttp1.send(null)
}

function jsg_snd1(inputString){
    if (xmlhttp1.readyState==4) {
        if (xmlhttp1.status == 200) {
                                            var compareString = xmlhttp1.responseText;
                                                        //alert(compareString)
            if(inputString==compareString){
                                                        //alert("Strings are equal");
                                                document.getElementById('text_zone').innerHTML += "; Ok "+inputString+"=="+compareString+"";
            }
        }
    }
}

Now the html in your body should look like:

    <tt id = 'text_go' onMouseUp="source1();" >Go!</tt>
    <tt id = 'text_zone' onMouseUp="text_zone.innerHTML = '';" >Click to clear!</tt>
    <input type ='text' id = 'text_zone1' onMouseUp="text_zone1.value = '';" value = 'Click to clear!' >

The extra stuf is for ___s & giggles.

JSG
  • 390
  • 1
  • 4
  • 13