H
hzgt9b
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.getElementsByTagName("to")[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.toString());
var span_to = document.getElementById("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.getElementsByTagName("to");
if (xmlObj) {
alert("xmlObj found");
} else {
alert("xmlObj null");
}
alert("number of \"to\" elements found: " +
xmlDoc.getElementsByTagName("to").length);
//HERE IS THE ERROR!
var xmlObj2 = xmlDoc.getElementsByTagName("to")[0];
if (xmlObj2) {
alert("xmlObj2 found");
} else {
alert("xmlObj2 null");
}
var xmlObj3 =
xmlDoc.getElementsByTagName("to")[0].childNodes[0];
if (xmlObj3) {
alert("xmlObj3 found");
} else {
alert("xmlObj3 null");
}
var xmlObj4 =
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
if (xmlObj4) {
alert("xmlObj4 found");
} else {
alert("xmlObj4 null");
}
}
function loadXML() {
//load xml file
// code for IE
if (window.ActiveXObject) {
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("note.xml");
getMessage();
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation &&
document.implementation.createDocument) {
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("note.xml");
xmlDoc.onload=getMessage;
} else {
alert('Your browser cannot handle this script');
}
}
function getMessage() {
debugTest();
document.getElementById("to").innerHTML=xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
document.getElementById("heading").innerHTML=xmlDoc.getElementsByTagName("heading")[0].childNodes[0].nodeValue;
} //getMessage
function navigateUrl(fullName) {
if (currentNode==fullName) { //do nothing when clicked on
same node
document.getElementById("status").innerHTML="Click
ignored! (<b>"+fullName+"</b> already active)";
} else { //navigate to the new node...
document.getElementById("status").innerHTML=""; //reset
status...
//set the current node to have a navy background
document.getElementById(fullName).style.cssText="background-color:
#4488DD; /*color: green;*/text-decoration: underline; /*font-weight:
bold;*/";
document.getElementById(fullName).className="currentNode";
previousNode=currentNode;
document.getElementById("prevNode").innerHTML="Previous
node=<b>"+previousNode+"</b>";
currentNode=fullName;
document.getElementById("currNode").innerHTML="Current
node=<b>"+currentNode+"</b>";
if (previousNode=='') { //no styles to change...
} else { //indicate that the previous slide has been
viewed...
document.getElementById(previousNode).className="visited";
}
}
} //end navigateUrl
function nodeHoverOn(fullName) {
if (fullName==currentNode) {
} else {
document.getElementById(fullName).style.cssText="background-color:
#FFFFFF; /*color: green; */text-decoration: underline; /*font-weight:
bold;*/";
}
} //end nodeHoverOn
function nodeHoverOff(fullName) {
document.getElementById(fullName).style.cssText="";
//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="navigateUrl(this.id)"
class="notVisited" onmouseover="window.status=''; nodeHoverOn(this.id)"
onmouseout="window.status=''; nodeHoverOff(this.id)">
To: <span id="to">Init value</span>
</div>
<div id="node2" title="Slide 2" onclick="navigateUrl(this.id )"
class="offclass" onmouseover="window.status=''; nodeHoverOn(this.id)"
onmouseout="window.status=''; nodeHoverOff(this.id)">
From: <span id="from">Init value</span>
</div>
<div id="node3" title="Objectives" onclick="navigateUrl(this.id)"
class="offclass" onmouseover="window.status=''; nodeHoverOn(this.id)"
onmouseout="window.status=''; nodeHoverOff(this.id)">
Subject: <span id="heading">Init value</span>
</div>
<div id="node4" title="End of Course"
onclick="navigateUrl(this.id)" class="offclass"
onmouseover="window.status=''; nodeHoverOn(this.id)"
onmouseout="window.status=''; nodeHoverOff(this.id)">
Message: <span id="message">Init value</span>
</div>
<br />
<div id="currNode">
</div>
<div id="prevNode">
</div>
<br />
<div id="status" style="background-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>Reminder</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=XMLDOMParsing_treeview.htm/>
<frame title="Content" id="showframe" name="showframe" />
</frameset>
</html>
*************************************************************
(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.getElementsByTagName("to")[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.toString());
var span_to = document.getElementById("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.getElementsByTagName("to");
if (xmlObj) {
alert("xmlObj found");
} else {
alert("xmlObj null");
}
alert("number of \"to\" elements found: " +
xmlDoc.getElementsByTagName("to").length);
//HERE IS THE ERROR!
var xmlObj2 = xmlDoc.getElementsByTagName("to")[0];
if (xmlObj2) {
alert("xmlObj2 found");
} else {
alert("xmlObj2 null");
}
var xmlObj3 =
xmlDoc.getElementsByTagName("to")[0].childNodes[0];
if (xmlObj3) {
alert("xmlObj3 found");
} else {
alert("xmlObj3 null");
}
var xmlObj4 =
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
if (xmlObj4) {
alert("xmlObj4 found");
} else {
alert("xmlObj4 null");
}
}
function loadXML() {
//load xml file
// code for IE
if (window.ActiveXObject) {
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("note.xml");
getMessage();
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation &&
document.implementation.createDocument) {
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("note.xml");
xmlDoc.onload=getMessage;
} else {
alert('Your browser cannot handle this script');
}
}
function getMessage() {
debugTest();
document.getElementById("to").innerHTML=xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
document.getElementById("heading").innerHTML=xmlDoc.getElementsByTagName("heading")[0].childNodes[0].nodeValue;
} //getMessage
function navigateUrl(fullName) {
if (currentNode==fullName) { //do nothing when clicked on
same node
document.getElementById("status").innerHTML="Click
ignored! (<b>"+fullName+"</b> already active)";
} else { //navigate to the new node...
document.getElementById("status").innerHTML=""; //reset
status...
//set the current node to have a navy background
document.getElementById(fullName).style.cssText="background-color:
#4488DD; /*color: green;*/text-decoration: underline; /*font-weight:
bold;*/";
document.getElementById(fullName).className="currentNode";
previousNode=currentNode;
document.getElementById("prevNode").innerHTML="Previous
node=<b>"+previousNode+"</b>";
currentNode=fullName;
document.getElementById("currNode").innerHTML="Current
node=<b>"+currentNode+"</b>";
if (previousNode=='') { //no styles to change...
} else { //indicate that the previous slide has been
viewed...
document.getElementById(previousNode).className="visited";
}
}
} //end navigateUrl
function nodeHoverOn(fullName) {
if (fullName==currentNode) {
} else {
document.getElementById(fullName).style.cssText="background-color:
#FFFFFF; /*color: green; */text-decoration: underline; /*font-weight:
bold;*/";
}
} //end nodeHoverOn
function nodeHoverOff(fullName) {
document.getElementById(fullName).style.cssText="";
//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="navigateUrl(this.id)"
class="notVisited" onmouseover="window.status=''; nodeHoverOn(this.id)"
onmouseout="window.status=''; nodeHoverOff(this.id)">
To: <span id="to">Init value</span>
</div>
<div id="node2" title="Slide 2" onclick="navigateUrl(this.id )"
class="offclass" onmouseover="window.status=''; nodeHoverOn(this.id)"
onmouseout="window.status=''; nodeHoverOff(this.id)">
From: <span id="from">Init value</span>
</div>
<div id="node3" title="Objectives" onclick="navigateUrl(this.id)"
class="offclass" onmouseover="window.status=''; nodeHoverOn(this.id)"
onmouseout="window.status=''; nodeHoverOff(this.id)">
Subject: <span id="heading">Init value</span>
</div>
<div id="node4" title="End of Course"
onclick="navigateUrl(this.id)" class="offclass"
onmouseover="window.status=''; nodeHoverOn(this.id)"
onmouseout="window.status=''; nodeHoverOff(this.id)">
Message: <span id="message">Init value</span>
</div>
<br />
<div id="currNode">
</div>
<div id="prevNode">
</div>
<br />
<div id="status" style="background-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>Reminder</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=XMLDOMParsing_treeview.htm/>
<frame title="Content" id="showframe" name="showframe" />
</frameset>
</html>
*************************************************************