Computer Science Canada

Loading a Poll with Javascript, need assistance with the FireFox Code.

Author:  chrisciscoioio [ Wed Oct 10, 2007 8:05 am ]
Post subject:  Loading a Poll with Javascript, need assistance with the FireFox Code.

Hello,

I am not sure if this fits in here, but it is web design.

I have some code samples I have put together, it creates a poll from data in an xml file, and places in inside a div.
The problem is that the examples that I used to create my code worked in every broswer, but when I try mine, they do not work.

So I need to figure out what went wrong, and you can only look at so many 1000's of googled asnwers before you give up.

Here is my code.
These files just need to be kept together in the same file and it will work in IE ( I used 7) but it will not work in My firefox, yet the example I based on will.

This is test.htm

code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-us">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title>Dynamically created radio buttons test, works in IE, not in anything else?</title>

<script>
// Start of loadXMLDoc()
function loadXMLDoc(dname)
{
// Variables
var xmlDoc;
// code for IE
if (window.ActiveXObject){xmlDoc=new ActiveXObject("Microsoft.XMLDOM");}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument){xmlDoc=document.implementation.createDocument("","",null);}
else{alert("Your browser cannot handle this script");}
xmlDoc.async=false;
xmlDoc.load(dname);
return(xmlDoc);
}
// End of loadXMLDoc()

// Start of displayCurrentPoll()
function displayCurrentPoll()
{
// Variables
var idRadio,typeRadio,onclickRadio;
var elementID, newRadio, newLabel, option, question;
var radio=new Array();
var label=new Array();
var space=new Array();
var i;
// Sets the attributes that are constant for all new elements
typeRadio='radio';
onclickRadio='updatePoll()';
// Load the xml file
xmlDoc=loadXMLDoc("poll.xml");
// Locate the xml elements
question=xmlDoc.getElementsByTagName('question');
option=xmlDoc.getElementsByTagName('option');
// Locate the place for the poll
elementID=document.getElementById('CurrentPoll');
// Write the question
label[0]=document.createElement('label');
space[0]=document.createElement('br');
label[0].htmlFor="Label";
label[0]=document.createTextNode(question[0].childNodes[0].nodeValue);
space[0].id ='Break';
elementID.appendChild(label[0]);
elementID.appendChild(space[0]);
// Create one radio button for each of the options
for (i=1;i<5;i++)// The value for the max times is hardcoded for now, except that to change
{
radio[i]=document.createElement('input');
label[i]=document.createElement('label');
space[i]=document.createElement('br');
radio[i].type= typeRadio;
radio[i].id='Radio';
radio[i].value='Choice';
radio[i].onclick=onclickRadio;
label[i].htmlFor='Label';
label[i]=document.createTextNode(option[0].childNodes[i-1].childNodes[0].nodeValue);
space[i].id ='Break';
elementID.appendChild(radio[i]);
elementID.appendChild(label[i]);
elementID.appendChild(space[i]);
}
}// End of displayCurrentPoll()</script>
</head>

<body onload="displayCurrentPoll();">

<div>
<form id="CurrentPoll"></form>
</div>

</body>
</html>



This is poll.xml

code:

<?xml version="1.0" encoding="iso-8859-1"?>
<poll>
        <question>Which Letter do you like the best.</question>
        <option>
                <A>A</A>
                <B>B</B>
                <C>C</C>
                <D>D</D>
        </option>
        <result>
                <A>0</A>
                <B>0</B>
                <C>0</C>
                <D>0</D>
        </result>
</poll>



O yes, before I forget everything needs to be W3C.


: