views:

274

answers:

4

Hi,

Im trying to parse a HTML result of **XmlHttpRequest** in Firefox. Im expecting to receive the HTML result from XmlHttpRequests *responseText* but when Im calling an alert(responseText) nothing is displayed. Ive followed the example from http://stackoverflow.com/questions/888875/how-to-parse-html-from-javascript-in-firefox but that doesnt work either. Here is thecode to make myself clear:

<html>
<head>
<script type="text/javascript">
var http1;
var result;
function onPageLoad()
{
 http1=getXmlHttpObject();

 http1.open("GET", "https://login.yahoo.com/config/login_verify2?&amp;.src=ym", true);
 http1.send(null);
 http1.onReadyStateChange=stateChanged();
}

function stateChanged()
{
 if(http1.readyState==4)
  {     
   result = http1.responseText;
   alert("result"+ result);
   var tempDiv = document.createElement('div');
   tempDiv.innerHTML = result.replace(/<script(.|\s)*?\/script>/g, '');
   // tempDiv now has a DOM structure:

   alert(tempDiv.getElementById('username').size);
  }
 else
  alert("mircea geoana la zoo");
}

function getXmlHttpObject()
{
 var objXMLHttp=null;
 if (typeof XMLHttpRequest!= 'undefined')
 {
  objXMLHttp=new XMLHttpRequest();
 } 
 else
 {
  objXMLHttp=new ActiveXObject(Microsoft.XmlHttp);
 }
 return objXMLHttp;
}
</script>
</head>
<body onload="onPageLoad()">
<p>aaa<p>
</body>
</html>
+2  A: 
http1.onReadyStateChange=stateChanged();

should be

http1.onReadyStateChange=stateChanged;
James McCormack
if I remove the (), stateChanged wont be called at all !!
claudiu
@claudiu: yes it will. You are supposed to assign a function reference, which is then called by the XHR object when a state change occurs. At the moment you are assigning "undefined", as that is the return value of calling stateChanged. But more to the point, as Tubbe says, you're going to get an error anyway as you can't use XHR across domains.
NickFitz
A: 

this is how you should define the xmlhttp Object:

  var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }

Check out this w3School tutorial to read about how to properly use AJAX calls and the like.

edit - my bad, only saw the line defining the call for IE6/5. Either way this method is much more clean.

Derleek
A: 

I see a big mistake there.. the message on the else branch should read 'miRcea', not 'micea'.. Tell me if this solves your issue, Mr claudiu ;))

Ade
already solved that big issue, but thanks for the observation Mrs Ade
claudiu
A: 

You can only send AJAX requests to the same domain from which the JavaScript originates. And I'm guessing you're not sending your requests from "login.yahoo.com"...

Tubbe
I don`t know what to think anymore, from what I`ve read on many forums/tutorials there`s no way I can access HTML objects from external links (other than the one from where the js script runs), BUT with XmlHttpRequest this could be possible in some way :|
claudiu
How about an AJAX call back to your webserver app that makes a call to the external destination (i.e yahoo) from there and just returns the resulting HTML?
James McCormack