Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

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 01-10-2013, 01:31 PM   PM User | #1
Henry.
New to the CF scene

 
Join Date: Jan 2013
Location: London, UK
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Henry. is an unknown quantity at this point
Restrictions on session only cookies

Is it ok to have more than one session cookie on a page/site (obviously with different names)? Are there any restrictions on this or any special actions to take?

I ask because I've one session cookie working perfectly, but the second one just doesn't want to work.
Henry. is offline   Reply With Quote
Old 01-10-2013, 05:07 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,392
Thanks: 18
Thanked 351 Times in 350 Posts
sunfighter is on a distinguished road
Yes there is a limit on cookies. I think all browsers limit them to 20 per domain.
Some browsers IE, opera have a 4K limit on the total amount of space taken up.
I'm sure you don't max this out with only two. Post code for the setting and reading of these cookies please.
sunfighter is offline   Reply With Quote
Old 01-11-2013, 04:22 AM   PM User | #3
Henry.
New to the CF scene

 
Join Date: Jan 2013
Location: London, UK
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Henry. is an unknown quantity at this point
Code:
<script type="text/javascript">
var min=12;
var max=24;
function zoomLetter(mysize) {
   var p = document.getElementsByTagName('p');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else if(mysize>2) {
		  var s = mysize;
	  }else{
         var s = 16;
      }
	  if (mysize ==1){
      	if(s!=max) {
         s += 1;
      }
	  }
	   if (mysize ==-1){
	   		if(s!=min) {
         		s -= 1;
      }
	  }
	  if (mysize >2){
		  s=mysize;
	  }
      p[i].style.fontSize = s+"px"
	  }
document.cookie="magnif="+s;
}

 //Get cookie routine by Shelley Powers 
 function changeBGC(color){
document.bgColor = color;
document.cookie="bgcolor="+color;
 }
function get_cookie(Name) {
 var search = Name + "="
 var returnvalue = "";
 if (document.cookie.length > 0) {
 offset = document.cookie.indexOf(search)
 // if cookie exists
 if (offset != -1) { 
offset += search.length
 // set index of beginning of value
 end = document.cookie.indexOf(";", offset);
 // set index of end of cookie value
 if (end == -1) end = document.cookie.length;
 returnvalue=unescape(document.cookie.substring(offset, end))
 }
 }
 return returnvalue;
 }

if (get_cookie("bgcolor")!="")
changeBGC(get_cookie("bgcolor"))

if (get_cookie("magnif")!="")
zoomLetter(get_cookie("magnif"))

 </script> 


</head>
 <body>
<div id="topbox"><img src="WFDA-headerright.gif" width="574" height="291" alt="headerright" /></div>
<div id="welcome">
 <p>Welcome<br /><br />
Welcome to the Waltham Forest Dyslexia Association (WFDA).  We are a grassroots charity that has been running since 1989 set up by concerned individuals – both parents of children with dyslexia and by teachers that specialise in dyslexia. Although we are affiliated with the British Dyslexia   Associaiton (BDA) we are dependent on membership subscriptions, grants and fundraising.</p> </div>

<div id="welcome2">
 <p>Our Vision<br /><br />
 WFDA is committed to sending out a lifeline to people with dyslexia who often have nowhere else to turn. Our vision is that we will live in a dyslexic friendly society that enables dyslexic people of all ages to reach their full potential. </p>
</div>
<div id="welcome3">
  <p>Our Aim <br /><br />
To support the needs of dyslexic children, young people and adults by providing an accessible, affordable, quality service run by appropriately trained staff. Also to advance the education of dyslexic people of all ages to help them to improve their confidence and independence as well as their well-being.</p>
</div>
<div id="header"><img src="WFDA-header.gif" width="630" height="291" alt="WFDA logo" /></div>
<div id="picture1">
 
</div>
<div id="background"><strong>Background colour</strong>
  <table width="307" border="0" align="center" cellpadding="2">
    <tr> <td width="13" bgcolor="#cccccc"><a href="#" onClick="javascript:changeBGC('#cccccc')">&nbsp;&nbsp; </a> </td>
       <td width="13" bgcolor="#ccccff"><a href="#" onClick="javascript:changeBGC('#ccccff')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#ccffff"><a href="#" onClick="javascript:changeBGC('#ccffff')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#99ffcc"><a href="#" onClick="javascript:changeBGC('#99ffcc')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#3dffdd"><a href="#" onClick="javascript:changeBGC('#3dffdd')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#88dd00"><a href="#" onClick="javascript:changeBGC('#88dd00')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#ccff00"><a href="#" onClick="javascript:changeBGC('#ccff00')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#eeee00"><a href="#" onClick="javascript:changeBGC('#eeee00')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#ffcc00"><a href="#" onClick="javascript:changeBGC('#ffcc00')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#cc9999"><a href="#" onClick="javascript:changeBGC('#cc9999')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#ff9999"><a href="#" onClick="javascript:changeBGC('#ff9999')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#ff99ff"><a href="#" onClick="javascript:changeBGC('#ff99ff')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#ffcccc"><a href="#" onClick="javascript:changeBGC('#ffcccc')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#ffffbb"><a href="#" onClick="javascript:changeBGC('#ffffbb')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#FFFFFF"><a href="#" onClick="javascript:changeBGC('#FFFFFF')">&nbsp;&nbsp; </a></td>      
</tr>
  </table>
  <table width="108" border="0"  align="center" cellpadding="2">
  <tr>
    <td width="50" height="49"> <h1 id="sprytrigger1"><a href="#" onclick="zoomLetter(1)">A+</a></h1></td>
    <td width="44"><h3 id="sprytrigger2"><a href="#" onclick="zoomLetter(-1)">A-</a></h3></td>
  </tr>
</table>
Both changeBGC and zoomLetter work within the page, but only the background colour is copied to another page when that's opened. The other page has the same script and zoomLetter works perfectly within that page.
For clarification, zoomLetter allows the user to increase or decrease the font size, one px at a time, between 14 and 24px.

I get the impression that either the cookie isn't stored or isn't called when the new page opens.

Last edited by Henry.; 01-11-2013 at 04:35 AM.. Reason: error in code..wasn't an error!
Henry. is offline   Reply With Quote
Old 01-11-2013, 03:37 PM   PM User | #4
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,392
Thanks: 18
Thanked 351 Times in 350 Posts
sunfighter is on a distinguished road
The first thing when loading a page is to look for the cookies. You could use:
<body onload="checkCookie()">

reading a cookie is not a straight foreword thing. read this http://www.w3schools.com/js/js_cookies.asp

P.S. I used Firefox to check for the cookies and yours are set.
sunfighter is offline   Reply With Quote
Old 01-12-2013, 02:22 AM   PM User | #5
Henry.
New to the CF scene

 
Join Date: Jan 2013
Location: London, UK
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Henry. is an unknown quantity at this point
Quote:
Originally Posted by sunfighter View Post
The first thing when loading a page is to look for the cookies. You could use:
<body onload="checkCookie()">
I tried that, thanks, but it still doesn't work

Quote:
Originally Posted by sunfighter View Post
reading a cookie is not a straight foreword thing. read this http://www.w3schools.com/js/js_cookies.asp
This is where I got my information about session cookies from.
http://www.javascriptkit.com/javatutors/cookie.shtml


Quote:
Originally Posted by sunfighter View Post
P.S. I used Firefox to check for the cookies and yours are set.
Thanks for that, sunfighter.
Henry. is offline   Reply With Quote
Old 01-12-2013, 06:55 PM   PM User | #6
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,392
Thanks: 18
Thanked 351 Times in 350 Posts
sunfighter is on a distinguished road
zoomLetter(mysize) does not seem to be working so I just made up a value of 2 to get a cookie for it. Your functions to make a cookie are the top two functions. I then added the w3shools code for reading them. did a on load thing and instead of changing the font and bgcolor I just used an alert to show you what the cookies are set to.

You can change the magnif to what ever you want to check it. But this is how it's done.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>New document</title>
<script type="text/javascript">
function zoomLetter(mysize) {
document.cookie="magnif="+8;
}
 function changeBGC(color){
document.bgColor = color;
document.cookie="bgcolor="+color;
 }

/*AND NOW*/
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
  {
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function checkCookie()
{

var magnify = getCookie("magnif");
var thecolor = getCookie("bgcolor");
if (magnify != null && magnify != "" && thecolor != null && thecolor != "")
	alert("the magnify is "+magnify+" and the color is "+thecolor);
}
</script>
<body onload="checkCookie()">

<div id="topbox"><img src="WFDA-headerright.gif" width="574" height="291" alt="headerright" /></div>
<div id="welcome">
 <p>Welcome<br /><br />
Welcome to the Waltham Forest Dyslexia Association (WFDA).  We are a grassroots charity that has been running since 1989 set up by concerned individuals – both parents of children with dyslexia and by teachers that specialise in dyslexia. Although we are affiliated with the British Dyslexia   Associaiton (BDA) we are dependent on membership subscriptions, grants and fundraising.</p> </div>

<div id="welcome2">
 <p>Our Vision<br /><br />
 WFDA is committed to sending out a lifeline to people with dyslexia who often have nowhere else to turn. Our vision is that we will live in a dyslexic friendly society that enables dyslexic people of all ages to reach their full potential. </p>
</div>
<div id="welcome3">
  <p>Our Aim <br /><br />
To support the needs of dyslexic children, young people and adults by providing an accessible, affordable, quality service run by appropriately trained staff. Also to advance the education of dyslexic people of all ages to help them to improve their confidence and independence as well as their well-being.</p>
</div>
<div id="header"><img src="WFDA-header.gif" width="630" height="291" alt="WFDA logo" /></div>
<div id="picture1">

</div>
<div id="background"><strong>Background colour</strong>
  <table width="307" border="0" align="center" cellpadding="2">
    <tr> <td width="13" bgcolor="#cccccc"><a href="#" onClick="javascript:changeBGC('#cccccc')">&nbsp;&nbsp; </a> </td>
       <td width="13" bgcolor="#ccccff"><a href="#" onClick="javascript:changeBGC('#ccccff')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#ccffff"><a href="#" onClick="javascript:changeBGC('#ccffff')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#99ffcc"><a href="#" onClick="javascript:changeBGC('#99ffcc')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#3dffdd"><a href="#" onClick="javascript:changeBGC('#3dffdd')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#88dd00"><a href="#" onClick="javascript:changeBGC('#88dd00')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#ccff00"><a href="#" onClick="javascript:changeBGC('#ccff00')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#eeee00"><a href="#" onClick="javascript:changeBGC('#eeee00')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#ffcc00"><a href="#" onClick="javascript:changeBGC('#ffcc00')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#cc9999"><a href="#" onClick="javascript:changeBGC('#cc9999')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#ff9999"><a href="#" onClick="javascript:changeBGC('#ff9999')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#ff99ff"><a href="#" onClick="javascript:changeBGC('#ff99ff')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#ffcccc"><a href="#" onClick="javascript:changeBGC('#ffcccc')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#ffffbb"><a href="#" onClick="javascript:changeBGC('#ffffbb')">&nbsp;&nbsp; </a></td>
       <td width="13" bgcolor="#FFFFFF"><a href="#" onClick="javascript:changeBGC('#FFFFFF')">&nbsp;&nbsp; </a></td>
</tr>
  </table>
  <table width="108" border="0"  align="center" cellpadding="2">
  <tr>
    <td width="50" height="49"> <h1 id="sprytrigger1"><a href="#" onclick="zoomLetter(1)">A+</a></h1></td>
    <td width="44"><h3 id="sprytrigger2"><a href="#" onclick="zoomLetter(-1)">A-</a></h3></td>
  </tr>
</table>

</body>
</html>
sunfighter is offline   Reply With Quote
Users who have thanked sunfighter for this post:
Henry. (01-12-2013)
Old 01-12-2013, 11:26 PM   PM User | #7
Henry.
New to the CF scene

 
Join Date: Jan 2013
Location: London, UK
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Henry. is an unknown quantity at this point
Thanks very much, sunfighter, that did the trick...after a couple of weeks of my struggling on my own.
Henry. 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 11:23 PM.


Advertisement
Log in to turn off these ads.