...

View Full Version : js.script how to do ?



guowei1000
06-15-2004, 07:33 AM
how can i put my function in another file? i saw other ppl doing this using something.js so can anyone teach me how? thanks :P being asking alot of silly qus lately :p

glenngv
06-15-2004, 07:44 AM
Just put your functions in a file and normally you would name it with a .js extension. Remember not to put <script></script> tags in it as well as HTML comment tags <!-- -->

And then to include in your html pages

<script type="text/javascript" src="theFile.js"></script>

This means that the external js is in the same directory as the page. Modify it appropriately if in the different location.

Then you can call the functions in your pages as you normally do as if the script is inline.

guowei1000
06-15-2004, 08:23 AM
it keep saying object expected..... :confused:

glenngv
06-15-2004, 08:39 AM
It means that it cannot find the function. Can you post the external js and how you include and call it?

guowei1000
06-15-2004, 08:46 AM
This is my external .js


function TestChildren(nodes) // write the first layer out
{
document.writeln("<table border = 1>");
document.writeln("<tr>");
document.writeln("<td>");
document.writeln("<A onClick='javaScript:toogle_state(this)'><font color= red><IMG SRC='minus.gif'>"+ nodes.nodeName +"</font></A>");
if (nodes.hasChildNodes)
{
document.writeln("<DIV>")
for (var i=0; i < nodes.childNodes.length; i++) // write second layer out then pass the third layer to the writechildren() funtion
{
document.writeln("<table border = 1>");
document.writeln("<tr>");
document.writeln("<td>");
if (nodes.childNodes(i).hasChildNodes) // add toogle in
{
document.writeln("<font color= red><A onClick='javaScript:toogle_state(this)'><IMG SRC='minus.gif'>"+nodes.childNodes(i).nodeName +"</A></font>");
writechildren(nodes.childNodes(i))
}
else

{
document.writeln(nodes.childNodes(i).nodeName);
}
document.writeln("</td>");
document.writeln("</tr>");
document.writeln("</table>");
}
document.writeln("</DIV>")
}
document.writeln("</td>");
document.writeln("</tr>");
document.writeln("</table>");
}



function writechildren(sianz)
{
if (sianz.hasChildNodes)
{
document.writeln("<DIV>")
for (var i=0; i < sianz.childNodes.length; i++) // check the length of the childNOdes
{
document.writeln("<table border = 1>");
document.writeln("<tr>");
document.writeln("<td>");
if (sianz.childNodes(i).hasChildNodes) // add toogle in
{
if (sianz.childNodes(i).nodeName == "#text")
{
document.writeln("<font color= red><A onClick='javaScript:toogle_state(this)'><IMG SRC='minus.gif'>"+ sianz.childNodes(i).text + "</A></font>");
writechildren(sianz.childNodes(i)) // if this layer got children then pass up so this layer of children can be written
}
else
{
document.writeln("<font color= red><A onClick='javaScript:toogle_state(this)'><IMG SRC='minus.gif'> "+ sianz.childNodes(i).nodeName + "</A></font>");
writechildren(sianz.childNodes(i))
}
}
else
{
if (sianz.childNodes(i).nodeName == "#text") // no children come here
{
document.writeln(sianz.childNodes(i).text);
writechildren(sianz.childNodes(i))
}
else
{
document.writeln(sianz.childNodes(i).nodeName);
writechildren(sianz.childNodes(i))
}
}
document.writeln("</td>");
document.writeln("</tr>");
document.writeln("</table>");
}
document.writeln("</DIV>")
}
}

this is my main html

<html>
<head>
<script type="text/javascript src="buildtree.js">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.load("note.xml");

function toogle_state(nodeCounter) // this toogle the state and pass info to the sideView.html side
{
var location = nodeCounter.sourceIndex // locate the location of the nodeCounter
var locationIMG = document.all[location + 1] //this locate the IMG tag for this
var nextDivLocation = document.all[location+2] // location the DIV
if (nextDivLocation.style.display =="")
{
nextDivLocation.style.display = "none"
locationIMG.src = "plus.gif";
}
else
{
nextDivLocation.style.display = ""
locationIMG.src = "minus.gif";
parent.frames['sidethingy'].document.getElementById("theId").innerHTML=nextDivLocation.innerText;
}
}
function callReadXml()
{
root = xmlDoc.documentElement;
TestChildren(root);
}</script>
</head>
<body>
<script type="text/javascript">
callReadXml();
</script>
</body>
</html>

is use the bold part to call.

glenngv
06-15-2004, 09:20 AM
Change this:

<script type="text/javascript src="buildtree.js">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
...
</script>

to:

<script type="text/javascript" src="buildtree.js"></script>
<script type="text/javascript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
...
</script>

If you include an external js, there should be no codes inside the script tag and should have separate script tag with internal scripts.

guowei1000
06-15-2004, 09:29 AM
:O it worked! omg still so much to learn about javascript



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum