document.write

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wangers16
    New Member
    • Jul 2007
    • 57

    #1

    document.write

    Hi,

    I am trying to get javascript to write the following code into my page, however it will not show

    Code:
    <ul class="basictab">
    	<li class="selected"><a href="my menu test.htm">Home</a></li>
    	<li><a href="slashdot_menu.html">Slashdot Menu</a></li>
    	<li><a href="block_menu.htm">Block Menu</a></li>
    	<li><a href="shadow_test.htm">Shadow Test</a></li>
    </ul>
    This is the javascript that I have have tried
    Code:
    <!--
    function insertmenu(text){
    var object = text.className
    object.className = "selected";
    }
    function writeJS(){
    var str='';
    str+='<ul class="basictab">';
    str+='<li name="home" class=""><a href="my menu test.htm">Home<\/a><\/li>';
    str+='<li name="slashdot" class=""><a href="slashdot_menu.html">Slashdot Menu<\/a><\/li>';
    str+='<\/ul>
    document.write(str);
    }
    //-->
    The insertmenu function needs to be able to change the class of the chosen list item to selected

    Could you please help

    Thnx in advance
  • pronerd
    Recognized Expert Contributor
    • Nov 2006
    • 392

    #2
    What calls these methods, and when? If you call document.write( ) after the page has loaded, what you are doing is over writing the entire page with that HTML.

    You may want to try setting .innerHTML to add code.

    Code:
        var someElement = document.getElementById('someTagID');
        someElement.innerHTML = '<<YOUR HTML HERE>>'


    Originally posted by wangers16
    Hi,
    The insertmenu function needs to be able to change the class of the chosen list item to selected
    So are you saying this currently does not work? The code looks correct. What event is it triggering on?

    Comment

    • wangers16
      New Member
      • Jul 2007
      • 57

      #3
      it needs to write it onload along with all of the other html as it is for a horizontal menu

      p.s. I have the same script without the insertmenu function on some of my other pages only without the list and that works ok as it is just working with hyperlinks

      Comment

      • pronerd
        Recognized Expert Contributor
        • Nov 2006
        • 392

        #4
        You didnt answer any of my questions. There is not much we can tell you without at lest some details. How specificly is this being called. To avoid confustion it is easiest to provide the code that calls them. (Inside code tags.)

        Are both functions being called at the same time? What happens when the are called? Error messages of any kind?

        If it works on other pages without the insert function, have you tried removing the insert function on this page to see if it works then? Is the object that is passed to the insert function being called before the tag is on the page? i.e. Is it called inline, and above it in the code?

        Comment

        • wangers16
          New Member
          • Jul 2007
          • 57

          #5
          this is the current coding inside my page

          Code:
          <script language="Javascript" src="js/linktop.js">
          <!--
          writeJS();
          insertmenu('home');
          //-->
          </script>
          Other html
          and I have tried it without the insert function yet it still doesn't work, it just doesn't want to write out the list

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            When you use the src attribute of the script tag, you can't add some more JavaScript within the script tags. Close them and start new ones for the inline code:
            [CODE=javascript]
            <script type="text/javascript" src="js/linktop.js"></script>
            <script type="text/javascript">
            writeJS();
            insertmenu('hom e');
            </script>[/CODE]

            Comment

            • wangers16
              New Member
              • Jul 2007
              • 57

              #7
              it's working gr8 now thnx for all of ur help

              Comment

              • wangers16
                New Member
                • Jul 2007
                • 57

                #8
                last post was incorrect I had forgotten to remove something which made me think that it was working sorry, it is still not working and it comes up with an error "'undefined ' is null or not an object" on line 4 of the script

                Comment

                • Death Slaught
                  Top Contributor
                  • Aug 2007
                  • 1137

                  #9
                  Originally posted by wangers16
                  last post was incorrect I had forgotten to remove something which made me think that it was working sorry, it is still not working and it comes up with an error "'undefined ' is null or not an object" on line 4 of the script
                  I've got a code that does this at my house somewhere, i'll post it later.

                  Thanks, Death

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by wangers16
                    last post was incorrect I had forgotten to remove something which made me think that it was working sorry, it is still not working and it comes up with an error "'undefined ' is null or not an object" on line 4 of the script
                    You're passing a string to the insertmenu function, not an object. You need to get the className property of the li element, not a string. Give the li elements ids and then pass the id as the argument. Then use document.getEle mentById(passed _id) to get hold of the element.

                    Comment

                    • dangroza
                      New Member
                      • Dec 2007
                      • 13

                      #11
                      Hello!

                      Yes, as acoder suggested, you should use the id property of the element and use document.getEle mentById() to select that element, instead of name tag. But this can also be done using document.getEle mentsByName which returns an array of elements(in our case we will have only value in the array). So, your function should look like this:
                      Code:
                      function insertmenu(text){
                      	var object = document.getElementsByName(text);
                      	object[0].className = "selected";
                      }
                      My complete test page is this:
                      Code:
                      <html>
                      <head>
                      <script type="text/javascript">
                      function insertmenu_(text){
                      	var object = 	text.className;
                      	object.className = "selected";
                      }
                      function insertmenu(text){
                      	var object = document.getElementsByName(text);
                      	object[0].className = "selected";
                      }
                      
                      function writeJS(){
                      	var str='';
                      	str+='<ul class="basictab">';
                      	str+='<li name="home" class=""><a href="my menu test.htm">Home<\/a><\/li>';
                      	str+='<li name="slashdot" class=""><a href="slashdot_menu.html">Slashdot Menu<\/a><\/li>';
                      str+='<\/ul>';
                      document.write(str);
                      }
                      writeJS();
                      insertmenu('home');
                      </script>
                      </head>
                      <body>
                      </body>
                      </html>
                      Hope this answers your question.
                      Dan

                      Comment

                      • wangers16
                        New Member
                        • Jul 2007
                        • 57

                        #12
                        yes this is working great now thanks for all of your help

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Glad it helped. Post again if you have any more questions.

                          Comment

                          Working...