Forum Moderators: open
This technique doesn't, of course, provide persistence between sessions, as a cookie or URL-encoded string can.
[edited by: Rambo_Tribble at 2:11 pm (utc) on Oct. 12, 2004]
I'm interested to know how I could use parameters and window names to do this, but can you give an example of how that would work?
I went to the list apart site Bernard and found this article [#*$!.com...] I've glanced through it and I don't think it uses cookies but it does use javascript - is this a good way of doing it in your opinion?
Basically I only want three choices of text size - the size it is normally (font-size:80%;), and two bigger choices. I understand what I basically need to do is create two new stylesheets, right? and allow users to select the one of their choice? Does this mean that I must maintain three stylesheets - or am I overcomplicating things? I just want to find out the simplest and most user friendly way to do this before I get started!
Cheers,
Helen.
Assuming that you're changing the size of the font for the ENTIRE body, you could probably do:
function change_size(fontsize)
{
document.body.style.fontSize = fontsize + "%";
}
Obviously, change the "%" to whatever unit of measurement you're using.
This assumes that you are going to give the function a fontsize, as in the following case:
<select name = "pickasize" onchange = "change_size(this.options[selectedIndex].value)">
<option value = "">Choose a size:</option>
<option value = "20">Small</option>
<option value = "50">Medium</option>
<option value = "100">Large</option>
</select>
The next step is to give the url the parameter to maintain state, and to change the fontsize of the next page based on that parameter. I've given you a start, but unfortunately, work calls at the moment so I'll have to leave you in suspense! :) I"ll try to write more later if someone else hasn't solved your issue by then.
Here is my code:
in the head of the document...
<script language="JavaScript" type="text/JavaScript">
<!--
// text resizer
//see if cookie is set and do relevant text size...
var textsize = document.cookie;
if (textsize!= null) document.body.style.fontSize = textsize + "%";
//function to change the text size
function change_size(fontsize)
{
document.body.style.fontSize = fontsize + "%";
//set the cookie
var the_cookie = "textsize=" + fontsize;
var the_date = new Date("December 31, 2050");
var the_cookie_date = the_date.toGMTString();
the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie = the_cookie;
}
//-->
</script>
...and in the body...
<div class="textresizer"> Text Resizer
<a href="#" class="smalltxt" onClick="change_size(80); return false;">a</a> ¦
<a href="#" class="medtxt" onClick="change_size(100); return false;">a</a> ¦
<a href="#" class="largetxt" onClick="change_size(140); return false;">a</a>
</div> I think it it setting the cookie but it doesn't seem to be getting the value out again. Any further help would be appreciated. Thanks!
is your problem. If you set textsize to equal your entire cookie, you'll probably get something like:
myval=12px; instead of just 12px; Alert textsize to see what you are actually getting if you haven't already.
I've had a go at splitting my cookie name and value...
//see if cookie is set and do relevant text size...
if (document.cookie && document.cookie!= ""){
var textsize1 = document.cookie;
var textsize2 = textsize1.split("=");
var textsize = unescape(textsize2[1]);
document.body.style.fontSize = textsize + "%";
}
but it's still not working right - any thoughts?
The text resizes when I click on the link to resize the page, but if I refresh the page, it goes back to normal, but I want the cookie to remember the enlarged text size.
I hope that makes sense - I think I'm in a bit of a muddle myself!
Here's what I would do. This should be the external js:
function set_cookie(fontsize)
{
var the_cookie = "textsize=" + fontsize;
var the_date = new Date("December 31, 2050");
var the_cookie_date = the_date.toGMTString();
the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie = the_cookie;
}
function read_cookie()
{
var cookieInfo = document.cookie;
var cookieInfoArray = textsize1.split("=");
var textsize = parseInt(cookieInfoArray[1]);
change_size(textsize);
}
function change_size(val)
{document.body.style.fontSize = val + "%";}
function change_and_set(val)
{
change_size(val);
set_cookie(val);
}
In the html...
<body onload = "read_cookie()">
<div class="textresizer"> Text Resizer
<a href="#" class="smalltxt" onClick="change_and_set(80); return false;">a</a> ¦
<a href="#" class="medtxt" onClick="change_and_set(100); return false;">a</a> ¦
<a href="#" class="largetxt" onClick="change_and_set(140); return false;">a</a>
</div>
function set_cookie(fontsize)
{
var the_cookie = "textsize=" + fontsize;
var the_date = new Date("December 31, 2050");
var the_cookie_date = the_date.toGMTString();
the_cookie = the_cookie + ";expires=" + the_cookie_date;
document.cookie = the_cookie;
}
function read_cookie()
{
var textsize = ''
var cookieInfo = document.cookie;
var cookieInfoArray = textsize1.split("=");
textsize = parseInt(cookieInfoArray[1]);
return textsize;
}
function get_fontsize()
{
var textsize = read_cookie();
if(textsize!= '')
{change_size(textsize);}
}
function change_size(val)
{document.body.style.fontSize = val + "%";}
function change_and_set(val)
{
change_size(val);
set_cookie(val);
}
In the html...
<body onload = "get_fontsize()">
<div class="textresizer"> Text Resizer
<a href="#" class="smalltxt" onClick="change_and_set(80); return false;">a</a> ¦
<a href="#" class="medtxt" onClick="change_and_set(100); return false;">a</a> ¦
<a href="#" class="largetxt" onClick="change_and_set(140); return false;">a</a>
</div>
I just made the script a little more modular so that the read_cookie function wasn't doing much other than simply reading the cookie and returning a result. This way also, if there wasn't a new textsize, i.e. - the visitor didn't change the size, the default fontsize won't change.
However, things still don't appear to be working quite right, and I'm getting those 'error on page' things.
There was a problem with this part
function read_cookie()
{
var textsize = ''
var cookieInfo = document.cookie;
var cookieInfoArray = textsize1.split("=");
textsize = parseInt(cookieInfoArray[1]);
return textsize;
}
in which textsize1 was undefined - was I right to change it to
var cookieInfoArray = textsize.split("=");
Also, there now seems to be a problem with this part
function change_size(val)
{document.body.style.fontSize = val + "%";}
I'm getting the error 'invalid arguement'
If you could help again with these that would be great!
Cheers.
function read_cookie()
{
var textsize = ''
var cookieInfo = document.cookie;
var cookieInfoArray = cookieInfo.split("=");
textsize = parseInt(cookieInfoArray[1]);
return textsize;
}
I don't know what could be wrong with this part.
function change_size(val)
{document.body.style.fontSize = val + "%";}
Does the error occur when you are first trying to resize, i.e. - right after you've clicked the size link or does the error occur when you refresh the page and are thus getting the textsize from the cookie?
I've fixed the code and it now works fine in Firefox and IE5.
In IE6 is where I'm getting the error -
error on page
line: 28
character: 2
error:invalid argument
code:0
It does work in IE4, but upping the text size makes the page go crazy and lots of the text and navigation falls off the edge, including the text resizer iteself, which is a slight concern.
The percents you are using to change the size of the text are kinda large so I can see why the text goes all over the place upon resizing.
I think it's that because I put this in my code to test...
function getcookieinfo()
{
alert(document.cookie);
}
<a href="#" onClick="getcookieinfo();"return false;">wheres me cookie?</a>
I wonder why the cookie isn't getting set - I know that it was refusing cookies earlier, but I changed the setting to 'accept all cookies' so it's a bit of a mystery. I restarted IE as well.
Still the good news is - in general this appears to work (I'll test it out IE6 on another machine) - so I'd like to thank you very much for all your help :)
Helen.