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 3 of 3

Thread: More than once

  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    More than once

    I have this script:

    <Head>
    Code:
    <script type="text/javascript" language="javascript">
    <!--
    function createCookie(name,value,days) {
      if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
      }
      else var expires = "";
      document.cookie = name+"="+value+expires+"; path=/";
    }
    
    function readCookie(name) {
      var nameEQ = name + "=";
      var ca = document.cookie.split(';');
      for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
      }
      return null;
    }
    
    function eraseCookie(name) {
      createCookie(name,"",-1);
    }
    
    function hidemembers() {
      toggleBox('members','none');
      createCookie('members','none');
    }
    
    function showmembers() {
      toggleBox('members','block');
      createCookie('members','block');
    }
    
    function toggleBox(id, display) {
      // DOM3 = IE5, NS6
      if (document.getElementById) {
        document.getElementById(id).style.display = display;
      // Netscape 4
      } else if (document.layers) {
        document.layers[id].display = display;
      // IE 4
      } else if (document.all) { 
        document.all[id].style.display = display;
      }
    }
    
    window.onload = function() {
      var memberDisplay = readCookie('members');
      if (memberDisplay !== null) {
        toggleBox('members',memberDisplay);
      }
    };
    </head>
    <body>

    Code:
    <a href="javascript:showmembers()">Show</a> | <a href="javascript:hidemembers()">hide</a>
    
    <div id="members">content1</div>
    </body>

    However i need to use the same script for more than one divs, lets call them members2 and members3. When i use the script more than once in a page only one of the divs works, help!
    ----------------------------------------------
    ----------------------------------------------

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,307
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    You cannot have two or more Javascripts with the same function or variable names, as the second (or last) overwrites the first.

    You will have to go through and rename all these in some way e.g.

    function createCookie2(name2,value2,days2)

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, but can you tell me the things i need to change, i have been tryng and i
    cant so this :S
    ----------------------------------------------
    ----------------------------------------------


  •  

    Posting Permissions

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