Click to See Complete Forum and Search --> : Javascript & XML
conspic
02-10-2003, 08:16 AM
Hi,
I am writing some javascript that configures an object dependent upon the data contained within an external xml file. In IE6 this is working. In netsape7 I can also read data from the xml file. The problem is that the XML is taking time to be parsed and as a result code that should be processed after the xml has been read is being processed before the xml has completed reading in netscape resulting in my objects properties returning as undefined.
I have tried using setTimeout to control the flow, but I am ending up with a situation where netscape continues to show as loading forever despite the fact that xml has clearly loaded. A side effect of this appears to be that neither of the browsers subsequently allow a refresh.
Has anyone had any experience with this and might be able to offer me some advice or point me to some explanatory code fragments?
Thanks,
Conspic
khalidali63
02-10-2003, 09:38 AM
You might want to put your code here,it seems like a logicall error(ofcourse it could be anything else as well).
So I'd say post your code so that some one can take a look at the flow of the program.
cheers
Khalid
conspic
02-10-2003, 10:46 AM
Hi,
Below are the listings from two files. The first is the HTML. In IE6 this correctly retrieves data from the XML and uses it to set the properties of an object. These values are then output to the page to confirm that the process is working. In netscape thisis not the case. The only output to the page in netscape is "End of Page". Also the alert is not issued.
Netscape reports:
Error: items has no properties
Source File: file:///D:/new/iRadioButtons/test.htm
Line: 102
I believe this is due to that fact that the code is being executed before the XML has been parsed. I can not find away around this that doesn't mess up the way I want to use the rest of the code.
I apologise for the state ofthe code. I've been playing around with it and so it's quite likely that it is not as tidy as it should be.
Thanks in advance for any response,
Conspic.
FILE: test.html
-------------------------------------------------------
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script language="JavaScript">
<!--
var myRadioButtons;
function iControls_iRadioButtons()
{
this.group = null;
this.labelAlign = null;
this.displayOrientation = null;
this.setGroup = function(oItem) {
this.group = oItem;
}
this.getGroup = function() {
return this.group;
}
this.setLabelAlign = function(oItem) {
this.labelAlign = oItem;
}
this.getLabelAlign = function(oItem) {
return this.labelAlign;
}
this.setDisplayOrientation = function(oItem) {
this.displayOrientation = oItem;
}
this.getDisplayOrientation = function(oItem) {
return this.displayOrientation;
}
this.buttons = new Array()
return;
}
function iControls_iRadioButton(label, value, state, showState) {
if (!label)
{
this.label = null;
} else {
this.label = label;
}
this.value = value;
this.showState = showState;
this.state = state;
this.setLabel = function(oItem) {
this.label = oItem;
}
this.getLabel = function() {
return this.label;
}
this.setValue = function(oItem) {
this.value = oItem;
}
this.getValue = function() {
return this.value;
}
this.setState = function(oItem) {
this.state = oItem;
}
this.getState = function() {
return this.state;
}
this.setShowState = function(oItem) {
this.showState = oItem;
}
this.getShowState = function() {
return this.showState;
}
return;
}
function verifySupport(xFile) {
if (document.implementation && document.implementation.createDocument) {
xDoc = document.implementation.createDocument("", "theXdoc", null);
} else {
var msXML = new ActiveXObject("microsoft.xmldom");
msXML.async = false;
xDoc = msXML;
}
xDoc.load(xFile);
return true;
}
function init(xFile) {
myRadioButtons = new iControls_iRadioButtons();
if (verifySupport(xFile)) {
getData();
return;
}
}
function getData() {
var items = xDoc.getElementsByTagName("controlDefinition")[0];
var thisButtons = new Array();
var count = 0;
for (i = 0; i < items.childNodes.length; i++) {
if (items.childNodes[i].nodeType == 1) {
thisButtons[count] = items.childNodes[i].firstChild.nodeValue;
count++;
}
}
myRadioButtons.setGroup(thisButtons[0]);
myRadioButtons.setLabelAlign(thisButtons[1]);
myRadioButtons.setDisplayOrientation(thisButtons[2]);
return;
}
-->
</script>
</head>
<body>
<script language="JavaScript">
<!--
init('iRadio.xml')
var thisGroup = myRadioButtons.getGroup();
document.write(myRadioButtons.group + " --- " + myRadioButtons.labelAlign + " --- " + myRadioButtons.getDisplayOrientation());
alert("Show this");
-->
</script>
<p>End of Page</p>
</body>
</html>
-------------------------------------------------------
FILE: iRadio.xml
-------------------------------------------------------
<?xml version="1.0" encoding="iso-8859-1"?>
<iControl type="iRadioButtons" name="iRadioGroup1">
<controlDefinition>
<group>aGroup</group>
<labelAlign>right</labelAlign>
<displayOrientation>horizontal</displayOrientation>
</controlDefinition>
<iRadioButtons>
<iRadioButton id="button1">
<label>Option 1</label>
<value>opt1</value>
<showState>visible</showState>
<state>checked</state>
</iRadioButton>
<iRadioButton id="button2">
<label>Option 2</label>
<value>opt2</value>
<showState>visible</showState>
<state>unchecked</state>
</iRadioButton>
<iRadioButton id="button3">
<label>Option 3</label>
<value>opt3</value>
<showState>visible</showState>
<state>unchecked</state>
</iRadioButton>
<iRadioButton id="button4">
<label>Option 4</label>
<value>opt4</value>
<showState>visible</showState>
<state>unchecked</state>
</iRadioButton>
</iRadioButtons>
</iControl>
-------------------------------------------------------
Does anyone have any suggestions on this?
thanks