PDA

View Full Version : problem with script


oppenheimerm
01-07-2008, 02:00 PM
OK, so I was playing around with learning JavaScript and I have function that makes a request for a xml file I have stored locally. The function "sort of works" but has some undesirable result. This if the function:

<script type="text/javascript">
<!--
function MakeXMLHTTPCall()
{
try
{
var xmlHttpObj = CreateXmlHttpRequestObject()
if(xmlHttpObj)
{
xmlHttpObj.open("GET", "http://" + location.host + "/Javascript Playground/DataFile.xml", true);
xmlHttpObj.onreadystatechange = function()
{
if(xmlHttpObj.status == HTTPSTATUS_OK)
{
alert("URL exist!");
}
if(xmlHttpObj.readyState == READYSTATE_COMPLETE)
{
// Extract the response text here and place in the div element.
document.getElementById("divResults").childNodes[0].nodeValue=xmlHttpObj.responseText;
}
}
}
xmlHttpObj.send(null);
}
catch(Err_msg)
{
var err_msg = "Could not locate url!\n\n";
err_msg += "Error code: " + err_msg.description;
alert(err_msg);
}

}


While it "sort of works" I am not getting the desired result, in firefox it loops through my

if(xmlHttpObj.status == HTTPSTATUS_OK)
{
alert("URL exist!");
}

portion of the code with the alert showing up three times, then it loads the the data. In internet explorer It as me if I wish to debug the code. The debugger breaks on the same section of code, which indicates that the value of xmlHttpObj.status is equal to "". Which I don't quite understand. In the end if I just ignore the error in IE and firefox, it does eventually load the xml file. I would prefer to know what the error is with my code, rather than leave this error,
regards

:rolleyes:

rnd me
01-12-2008, 02:37 AM
i dont know if this is relevant (nothing else jumps out at me) ,but this is slightly incorrect due to capitalization:



catch(Err_msg)
{
var err_msg = "Could not locate url!\n\n";
err_msg += "Error code: " + err_msg.description;
alert(err_msg);

err_msg has no description property (it's a fresh string).
Err_msg (an error object) does have a .message property in both browsers you mention.


also,

try
document.getElementById("divResults").childNodes[0].innerHTML=
instead of
document.getElementById("divResults").childNodes[0].nodeValue=