Guys..
I am stuck with this & it is really confusing as I have tried all possible ways..
I have attached 3 files here – readXML.html, readXML.js and readXML.xml.
I am trying to do something as simple as this.. In my readXML.xml, I have a xml structure that has site name & site value. On my HTML, I allow the user to enter a site name & if that site name matches with the site name in XML (refer JavaScript), the matching site value is used to open a new window. For instance, the user enters CNN and “cnn.com” opens in a new window.
This works in IE but not in Firefox.
Also note I have Firebug installed for my Firefox & when I browse the xmlDom element in the DOM tab, it contains the site name & site values.. But, then why the JavaScript fails to get all the values from XML? Pls try this and can you help me find where the problem is..
Thanks.
readXML.xml
<?xml version=”1.0″ ?>
<sites>
<site>
<name>Hotmail</name>
<value>
</site>
<site>
<name>Yahoo</name>
<value>
</site>
<site>
<name>Gmail</name>
<value>
</site>
<site>
<name>CNN</name>
<value>
</site>
</sites>
readXML.js
var xmlDoc;
var sites = new Array();
importXML(“readXML.xml”);
// Import the XML
function importXML(file)
{
if (document.implementation && document.implementation.createDocument) // Firefox Check
{
xmlDoc = document.implementation.createDocument(“”, “”, null);
xmlDoc.load(“readXML.xml”);
//xmlDoc.onload = loadXML(xmlDoc);
loadXML(xmlDoc);
}
else if (window.ActiveXObject) // Internet Explorer Check
{
xmlDoc = new ActiveXObject(“Microsoft.XMLDOM”);
xmlDoc.async = false;
xmlDoc.load(“readXML.xml”);
//loadXML();
loadXML(xmlDoc);
}
else
{
alert (“Your browser cannot handle the script”);
return false;
}
}
/*
*/
function loadXML(xmlDoc)
{
if (xmlDoc != null )
{
var allSitesArray = xmlDoc.getElementsByTagName(“sites”);
if (allSitesArray == null || allSitesArray[0] == null)
{
return false;
}
var sitesArray = allSitesArray[0].getElementsByTagName(“site”);
for (var i = 0; i < sitesArray.length; i ++)
{
var siteName, siteValue = “”;
var siteArray = new Array();
siteNameArray = sitesArray[i].getElementsByTagName(“name”);
siteValueArray = sitesArray[i].getElementsByTagName(“value”);
siteName = siteNameArray[0].firstChild.nodeValue;
siteValue = siteValueArray[0].firstChild.nodeValue;
siteArray[0] = siteName;
siteArray[1] = siteValue;
sites.push(siteArray);
}
}
}
// Open another window with the matching site address, if the site name entered by the user matches the information that is stored.
function showSiteValue()
{
var siteNameInput = document.getElementById(“siteName”).value;
var matchFoundFlag = 0;
for (var i = 0; i < sites.length; i ++)
{
var siteArray = sites[i];
siteName = siteArray[0];
siteValue = siteArray[1];
if (siteName.toLowerCase() == siteNameInput.toLowerCase())
{
matchFoundFlag = 1;
window.open(siteValue);
return false;
}
}
if (matchFoundFlag == 0)
{
alert (“no matches found”);
setFocus();
return false;
}
}
/*
*/
function formSubmitOnEnterKeyPress(e)
{
// This variable holds the literal character code.
var characterCode;
// If this is a “Netscape” browser, perform the following.
if(e && e.which)
{
characterCode = e.which;
}
// If this is an “Internet Explorer” browser, perform the following.
else
{
characterCode = e.keyCode; //character code is contained in IE’s keyCode property
}
// If the character code is “13”, it means the “Enter” key is hit. Then, return true.
// Else, return false.
if (characterCode == 13)
{
showSiteValue();
return false;
}
}
// Focus on the search text
function setFocus()
{
document.getElementById(“siteName”).focus();
}
readXML.html
<html>
<head>
<title>Search Sites</title>
<script type=”text/javascript” src=”readXML.js”></script>
</head>
<body onload=”setFocus();”>
<form onkeypress=”formSubmitOnEnterKeyPress(event);” method=”post”>
<table>
<tr>
<td>Site : </td>
<td><input type=”text” name=”siteName” id=”siteName” maxlength=”10″ size=”10″ /></td>
</tr>
<tr>
<td colspan=”2″> </td>
</tr>
<tr>
<td colspan=”2″ align=”center”>
<input type=”submit” name=”submit” value=”Go” onclick=”showSiteValue();” />
</td>
</tr>
</table>
</form>
</body>
</html>
***********************************************?