I've written a simple javascript page that parses an XML file...
(Actually I just modified the "Parsing an XML File" sample from
http://www.w3schools.com/dom/dom_parser.asp)
The page works great standalone... but when I try to make this work
under frames I get "Error: Object required" when the following line
executes:
xmlDoc.getEleme ntsByTagName("t o")[0];
The standalone file is named treeView.htm (attached). You should be
able to copy and paste this into a simple editor, save it then open it
without (javascript) problems. The xml file is named note.xml and is
attached below. Finally, I've also attached the mainFrame.htm that I'm
trying to tie this together with... save it to the same folder as the
other two documents to replicate the "...Object required" error.
Can anyone tell me what's going on here? Further, I've noticed that my
styles work great stand-alone but fail under frames.
Any help would be appreciated.
celoftis
Code follows:
*************** ******treeView. htm************ *******
<html>
<head>
<title>TreeView </title>
<!--<script type="text/javascript" src="treeDecor. js"></script>-->
<script type="text/javascript">
var currentNode="";
var previousNode="" ;
var xmlDoc;
function debugTest() {
alert(xmlDoc.to String());
var span_to = document.getEle mentById("to");
if (span_to) {
alert("span_to found");
} else {
alert("span_to null");
}
var xdoc = xmlDoc
if (xmlDoc) {
alert("xmlDoc found");
} else {
alert("xmlDoc null");
}
var xmlObj = xmlDoc.getEleme ntsByTagName("t o");
if (xmlObj) {
alert("xmlObj found");
} else {
alert("xmlObj null");
}
alert("number of \"to\" elements found: " +
xmlDoc.getEleme ntsByTagName("t o").length);
//HERE IS THE ERROR!
var xmlObj2 = xmlDoc.getEleme ntsByTagName("t o")[0];
if (xmlObj2) {
alert("xmlObj2 found");
} else {
alert("xmlObj2 null");
}
var xmlObj3 =
xmlDoc.getEleme ntsByTagName("t o")[0].childNodes[0];
if (xmlObj3) {
alert("xmlObj3 found");
} else {
alert("xmlObj3 null");
}
var xmlObj4 =
xmlDoc.getEleme ntsByTagName("t o")[0].childNodes[0].nodeValue;
if (xmlObj4) {
alert("xmlObj4 found");
} else {
alert("xmlObj4 null");
}
}
function loadXML() {
//load xml file
// code for IE
if (window.ActiveX Object) {
xmlDoc=new ActiveXObject(" Microsoft.XMLDO M");
xmlDoc.async=fa lse;
xmlDoc.load("no te.xml");
getMessage();
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.imple mentation &&
document.implem entation.create Document) {
xmlDoc=document .implementation .createDocument ("","",null) ;
xmlDoc.load("no te.xml");
xmlDoc.onload=g etMessage;
} else {
alert('Your browser cannot handle this script');
}
}
function getMessage() {
debugTest();
document.getEle mentById("to"). innerHTML=xmlDo c.getElementsBy TagName("to")[0].childNodes[0].nodeValue;
document.getEle mentById("from" ).innerHTML=xml Doc.getElements ByTagName("from ")[0].childNodes[0].nodeValue;
document.getEle mentById("messa ge").innerHTML= xmlDoc.getEleme ntsByTagName("b ody")[0].childNodes[0].nodeValue;
document.getEle mentById("headi ng").innerHTML= xmlDoc.getEleme ntsByTagName("h eading")[0].childNodes[0].nodeValue;
} //getMessage
function navigateUrl(ful lName) {
if (currentNode==f ullName) { //do nothing when clicked on
same node
document.getEle mentById("statu s").innerHTML=" Click
ignored! (<b>"+fullName+ "</balready active)";
} else { //navigate to the new node...
document.getEle mentById("statu s").innerHTML=" "; //reset
status...
//set the current node to have a navy background
document.getEle mentById(fullNa me).style.cssTe xt="background-color:
#4488DD; /*color: green;*/text-decoration: underline; /*font-weight:
bold;*/";
document.getEle mentById(fullNa me).className=" currentNode";
previousNode=cu rrentNode;
document.getEle mentById("prevN ode").innerHTML ="Previous
node=<b>"+previ ousNode+"</b>";
currentNode=ful lName;
document.getEle mentById("currN ode").innerHTML ="Current
node=<b>"+curre ntNode+"</b>";
if (previousNode== '') { //no styles to change...
} else { //indicate that the previous slide has been
viewed...
document.getEle mentById(previo usNode).classNa me="visited";
}
}
} //end navigateUrl
function nodeHoverOn(ful lName) {
if (fullName==curr entNode) {
} else {
document.getEle mentById(fullNa me).style.cssTe xt="background-color:
#FFFFFF; /*color: green; */text-decoration: underline; /*font-weight:
bold;*/";
}
} //end nodeHoverOn
function nodeHoverOff(fu llName) {
document.getEle mentById(fullNa me).style.cssTe xt="";
//remove inline style.... cascades back to internal, or external css
} //end nodeHoverOff
</script>
<style>
div,span {
color: black;
cursor: pointer;
}
..notVisited {
text-decoration: none;
background-color: transparent;
}
..visited {
text-decoration: none;
background-color: InactiveBorder;
}
..currentNode {
text-decoration: underline;
background-color: #4488DD;
}
</style>
</head>
<body onload="loadXML ()" scroll=no>
<div id="node1" title="Question 1" onclick="naviga teUrl(this.id)"
class="notVisit ed" onmouseover="wi ndow.status=''; nodeHoverOn(thi s.id)"
onmouseout="win dow.status=''; nodeHoverOff(th is.id)">
To: <span id="to">Init value</span>
</div>
<div id="node2" title="Slide 2" onclick="naviga teUrl(this.id )"
class="offclass " onmouseover="wi ndow.status=''; nodeHoverOn(thi s.id)"
onmouseout="win dow.status=''; nodeHoverOff(th is.id)">
From: <spa n id="from">Init value</span>
</div>
<div id="node3" title="Objectiv es" onclick="naviga teUrl(this.id)"
class="offclass " onmouseover="wi ndow.status=''; nodeHoverOn(thi s.id)"
onmouseout="win dow.status=''; nodeHoverOff(th is.id)">
Subject: < span id="heading">In it value</span>
</div>
<div id="node4" title="End of Course"
onclick="naviga teUrl(this.id)" class="offclass "
onmouseover="wi ndow.status=''; nodeHoverOn(thi s.id)"
onmouseout="win dow.status=''; nodeHoverOff(th is.id)">
Message: < span id="message">In it value</span>
</div>
<br />
<div id="currNode">
</div>
<div id="prevNode">
</div>
<br />
<div id="status" style="backgrou nd-color: InactiveBorder" ></div>
</body>
</html>
*************** *************** *************** *************
*************** *****note.xml** *************** ************
<?xml version="1.0" encoding="iso-8859-1" ?>
<!-- Edited with XML Spy v2007 (http://www.altova.com) -->
<note time="12:03:46" >
<to>Tove</to>
<from>Jani</from>
<heading>Remind er</heading>
<body>Don't forget me this weekend!</body>
</note>
*************** *************** *************** *************
*************** **mainFrame.htm *************** ********
<html>
<head ></head>
<frameset id="mainFrame" cols="33%, *">
<frame title="TreeView " id="tv" name="tv"
src=XMLDOMParsi ng_treeview.htm/>
<frame title="Content" id="showframe" name="showframe " />
</frameset>
</html>
*************** *************** *************** *************** *