Connecting Tech Pros Worldwide Forums | Help | Site Map

JS works in IE but not FF

Newbie
 
Join Date: May 2007
Posts: 6
#1: May 22 '07
I've seen numerous iterations of this problem, but none of the proposed solutions work for me. When I use getElementsByTag() on an XML document, IE returns 2 hits but FF returns 0. I'll post both the loading function and relevant functions:

[PHP]function loadXML(){
// code for IE
if (window.ActiveXObject){
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("twl.xml");
oddBrowser=false;
setupTeam();
}

// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation &&
document.implementation.createDocument){
oddBrowser = true;
xmlDoc=document.implementation.createDocument(""," ",null);
xmlDoc.load("twl.xml");
xmlDoc.onload = setupTeam();
}
else
{
alert('Your browser cannot handle this script');
}
}[/PHP]
[PHP]function setupTeam(){ //Fails in FF
debugit('Entered Team Setup')
var name;
var url;
name = getChild(xmlDoc,"name");
debugit('Name: ' + name);
url = getChild(xmlDoc,"url");
debugit('URL: ' + url);

strOut += "<tr><td>" + "<a href=\"" + url + "\">" + name + "\
</a></td></tr>";

setupLadders();
}[/PHP]
[PHP]function getChild(cNode,target){
var retNode;
debugit('cNode: ' + cNode.nodeName + ' Target: ' + target);
var numTgts = cNode.getElementsByTagName(target).length;
debugit('Number of \"' + target + '\"s in ' + cNode.nodeName + ': ' + numTgts);
var i = 0;
do{
debugit('Entered do loop');
retNode = cNode.getElementsByTagName(target)[0].childNodes[i];
debugit('Current retNode value: ' + retNode.nodeValue);
i++;
}while(retNode.nodeType!=3)
debugit('retNode: ' + retNode.nodeName);
return retNode.nodeValue;
}[/PHP]

I get to the getElementsByTagName(target)[0] and FF's error console says that it has no properties (I'm assuming because it doesn't exist). I'm not sure what is wrong. The only other thing is that this is all being done locally, with the HTML file and XML file loaded into a directory on my HDD. I run the script by viewing the HTML page locally.

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: May 22 '07

re: JS works in IE but not FF


Try running on a web server or even localhost.
Newbie
 
Join Date: May 2007
Posts: 6
#3: May 22 '07

re: JS works in IE but not FF


Unfortunately, when I ran it on a web server, I got the same results. :-(
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#4: May 23 '07

re: JS works in IE but not FF


Quote:

Originally Posted by LikwidN2

I get to the getElementsByTagName(target)[0] and FF's error console says that it has no properties (I'm assuming because it doesn't exist). I'm not sure what is wrong.

Does it fail regardless of the value of target, or are there certain targets that it won't find?

Is it a case-sensitivity issue? Try adding some '.toUpperCase()'s in there....
Newbie
 
Join Date: May 2007
Posts: 6
#5: May 24 '07

re: JS works in IE but not FF


The value passed as target is always exact case to the node name (everything is lowercase in the XML document). Heh, this is very frustrating.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: May 24 '07

re: JS works in IE but not FF


What does the XML file look like?

xmlDoc is global (clutching at straws maybe?)
Newbie
 
Join Date: May 2007
Posts: 6
#7: May 24 '07

re: JS works in IE but not FF


The XML file used (this program will actually be a Google Gadget, so I can take care of pulling an XML file off another domain without sever-side scripting) is hosted at another domain. I'm hoping to eventually use it for any team, but ours is located at http://www.teamwarfare.com/xml/viewt...ket+Clan%5D%5B.

xmlDoc is declared globally.

Thanks so much for being so thorough.
Newbie
 
Join Date: May 2007
Posts: 6
#8: May 24 '07

re: JS works in IE but not FF


I canned the modularity I was going for and wrote it inline. I'm going to post the script (It's not terribly long and commented fairly well). It works exactly as planned in IE but not FF. The whole point of the getChild() function was based on faulty understanding of Mozilla's implimentation of the DOM (I thought there could be whitespace nodes between an element and its text, but it only happens between an element and its child if the child is another element). Anyway:

[PHP] var response;

function run(){
//Load code for IE
if (window.ActiveXObject){
response=new ActiveXObject("Microsoft.XMLDOM");
response.async=false;
response.load("twl.xml");
oddBrowser=false;
}
//Code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument){
alert('Mozilla Mode');
response=document.implementation.createDocument("" ,"",null);
response.load("twl.xml");
}
else{
alert('Your browser cannot handle this script');
}

// Start building HTML string that will be displayed in <div>.
var html ="<table border=\"1\">";
var i = 0;

//Get team name
var name;
name = response.getElementsByTagName('name')[0].firstChild.nodeValue;

//Get team URL
var teamurl;
teamurl = response.getElementsByTagName('url')[0].firstChild.nodeValue;

//Output Team Info
html += "<tr><td>" + "<a href=\"" + teamurl + "\">" + name + "</a></td></tr>";

//Setup Laddrs
var ladders;
var maps;
var ladderName;
var opp;
var date;
var map;

//Set Ladders and Maps nodeList
ladders = response.getElementsByTagName('ladder')
maps = response.getElementsByTagName('map');

//Loop through each ladder
for (var i=0; i<ladders.length; i++){
//Get Opponent Name
opp = ladders[i].getElementsByTagName('opponentname')[0].firstChild.nodeValue;

//Get Match Date
date = ladders[i].getElementsByTagName('matchdate')[0].firstChild.nodeValue;

//Map
map=maps.item(i*2).attributes[0].nodeValue;

//Ladder Name
ladderName=ladders.item(i).attributes[0].nodeValue;

html += "<tr><td><table border=\"2\">"
html += "<tr><td>Ladder</td><td>" + ladderName + "</td></tr>";
html += "<tr><td>Date/Time</td><td>" + date + "</td></tr>";
html += "<tr><td>Opponent</td><td>" + opp + "</td></tr>";
html += "<tr><td>Map</td><td>" + map + "</td></tr>"
html += "</table></td></tr>";
}

// Close up Table and div
html += "</table>";
html += "</div>";
document.write(html);
}[/PHP]

Once again, thanks for your time and dedication, I owe you a lot.
Reply