Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-25-2007, 05:01 PM   PM User | #1
TaboTabo
New Coder

 
Join Date: Feb 2007
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
TaboTabo is an unknown quantity at this point
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!
__________________
----------------------------------------------
----------------------------------------------
TaboTabo is offline   Reply With Quote
Old 02-25-2007, 07:11 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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)
Philip M is offline   Reply With Quote
Old 02-25-2007, 10:24 PM   PM User | #3
TaboTabo
New Coder

 
Join Date: Feb 2007
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
TaboTabo is an unknown quantity at this point
Thanks, but can you tell me the things i need to change, i have been tryng and i
cant so this :S
__________________
----------------------------------------------
----------------------------------------------
TaboTabo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:16 PM.


Advertisement
Log in to turn off these ads.