I am creating a cookie when a user clicks on a link and saving a value based on the link clicked, everytime the user goes to a different page i want the JS to check if the cookie already exists and use the value stored in it or if not to then create the cookie and save the selected value. The cookie can also change value based on the different links that the user clicks. Please find below what i have so far: - function font_size(size)
-
{
-
var checkCookie = getCookie('testcookie');
-
if (checkCookie == null)
-
{
-
if(size=='small') {
-
//document.body.style.fontSize = '10';
-
setCookie('10');
-
}
-
else if (size=='normal')
-
document.body.style.fontSize = '12';
-
else
-
document.body.style.fontSize = '14';
-
}
-
}
-
function setCookie(size)
-
{
-
var theDate = new Date();
-
var oneYearLater = new Date( theDate.getTime() + 31536000000 );
-
var expiryDate = oneYearLater.toGMTString();
-
document.cookie = 'testcookie=test;expires='+expiryDate+';path=/';
-
}
-
function getCookie(cookie_name)
-
{
-
var nameEQ = cookie_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;
-
}
9 1907
In font_size(), you don't have a case where the cookie is set, and in setCookie, the cookie is always set to "test", not to the size parameter.
Does this make more sense? Sorry about my lack of JS understanding, never really used it until a few months ago. - function font_size(size)
-
{
-
var checkCookie = getCookie('testcookie');
-
if (checkCookie == null)
-
{
-
if(size=='small')
-
setCookie('10');
-
else if (size=='normal')
-
setCookie('12');
-
else
-
setCookie('14');
-
}
-
}
-
function setCookie(size)
-
{
-
var theDate = new Date();
-
var oneYearLater = new Date( theDate.getTime() + 31536000000 );
-
var expiryDate = oneYearLater.toGMTString();
-
document.cookie = 'testcookie='+size+';expires='+expiryDate+';path=/';
-
}
-
function getCookie(cookie_name)
-
{
-
var nameEQ = cookie_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;
-
-
}
Now you're setting the cookie, but not making use of it. Set the font size to the checkCookie value if it's set, i.e. when it's not null.
This is what I'm using now as I want the value in the cookie to be updated if the link on the site is clicked again, is this the correct way of doing it?
Now the one thing I've been unable to figure out it how to get the value from the cookie if the cookie exists and apply it to the site on load? - function font_size(size)
-
{
-
if(size=='small')
-
setCookie('10');
-
else if (size=='normal')
-
setCookie('12');
-
else
-
setCookie('14');
-
}
-
function setCookie(size)
-
{
-
var currentDate = new Date();
-
var expiryLength = new Date(currentDate.getTime() + 31536000000);
-
var expiryDate = expiryLength.toGMTString();
-
document.cookie = 'testcookie='+size+';expires='+expiryDate+';path=/';
-
}
Call a checkCookie() function onload, e.g. - function checkCookie() {
-
fontsize=getCookie('testcookie');
-
if (fontsize!=null) {
-
document.body.style.fontSize = fontsize + "px";
-
}
-
}
Thanks for that, got it all working fine now, heres my code for anyone in the future. - function font_size(size) {
-
if(size=='small') {
-
setCookie('10');
-
document.body.style.fontSize = "10px";
-
}
-
else if (size=='normal') {
-
setCookie('12');
-
document.body.style.fontSize = "12px";
-
}
-
else {
-
setCookie('14');
-
document.body.style.fontSize = "14px";
-
}
-
}
-
function setCookie(size) {
-
var currentDate = new Date();
-
var expiryLength = new Date(currentDate.getTime() + 31536000000);
-
var expiryDate = expiryLength.toGMTString();
-
document.cookie = 'testcookie='+size+';expires='+expiryDate+';path=/';
-
}
-
function getCookie(cookie_name) {
-
var nameEQ = cookie_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 checkCookie() {
-
fontsize = getCookie('testcookie');
-
if (fontsize!=null) {
-
document.body.style.fontSize = fontsize + "px";
-
}
-
}
No problem, glad to help :)
Quick question in relation to this, instead of hardcoding the font sizes like 10,12 and 14 can you just say if size is set to normal(12px) add 2px onto all font-sizes on the webpage or is it very difficult to do as there will be many different font sizes on the webpage?
You could do that, but you'd need to get all the separate elements with the different sizes and add 2px, e.g.: - document.body.style.fontSize = (parseInt(document.body.style.fontSize) + 2) + "px";
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ben Hearsum |
last post by:
I'm confused about the way Cookie.load outputs cookies. Is there a way I
can tell it to give me the value of a key?
I set my cookie like this:
mycookie = Cookie.SimpleCookie()
mycookie =...
|
by: brettr |
last post by:
When I reference document.cookie, there is a long string of key=value;
pairs listed. I may have 100 hundred cookies on my hard drive.
However, most only have one key=value pair. Does the...
|
by: socialism001 |
last post by:
I'm trying to store a value in a cookie but its not working. Can
anyone see what I might be doing wrong.
Thanks,
Chris
~~~~~~~~~~~~~~~~~~
<script language="javascript">...
|
by: Marco Krechting |
last post by:
Hi All,
I have a page with a list of hyperlinks.
I want to save information in a cookie about the fact that I entered an
hyperlink or not.
When I click one of the hyperlinks I want this stored...
|
by: ehendrikd |
last post by:
hi all
i need some clarification on how the php session work in relation to
cookies.
we have a web site where users need to log in. a few of our users were
having troubles with their browser...
|
by: Owen |
last post by:
I have a web app that is a mixture of ASP and ASP.NET pages. Largely the
only data passed between them is via the querystring, or by reading from a
database. However there is a requirement for...
|
by: csgraham74 |
last post by:
Hi,
Im trying to make changes to an appliction that i inherited. i have
set several cookies with different small values from XML. i get to a
piece of code where i response.redirect to another...
|
by: RootShell |
last post by:
I have been experiencing a lot of troubles in reading a PHP COOKIE from
javascript.
if i write the cookie and read it in javascript in the same PHP file it
works well, but if i write the cookie...
|
by: gcervantes |
last post by:
Hello, I am making a screen scraper, in other programs I have been able to log into a site, get the cookie, submit a form and read the results.
However, this time I am in troubles, the site I need...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |