Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    js.script how to do ?

    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

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,960
    Thanks
    0
    Thanked 236 Times in 233 Posts
    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.

  • #3
    New Coder
    Join Date
    May 2004
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it keep saying object expected.....

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,960
    Thanks
    0
    Thanked 236 Times in 233 Posts
    It means that it cannot find the function. Can you post the external js and how you include and call it?

  • #5
    New Coder
    Join Date
    May 2004
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,960
    Thanks
    0
    Thanked 236 Times in 233 Posts
    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.

  • #7
    New Coder
    Join Date
    May 2004
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    :O it worked! omg still so much to learn about javascript


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •