www.webdeveloper.com
Results 1 to 3 of 3

Thread: javascript changes css cookie to remember preference

Hybrid View

  1. #1
    Join Date
    Apr 2012
    Posts
    10

    javascript changes css cookie to remember preference

    Hi

    I have a number of sytle sheets which my visitors can than select through Javascript buttons. This works fine

    I would like to then use a cookie to remember the users preference when they change to another page on my site but I just cant get it to work

    the code I have which works for changing between style sheets is below, could anyone help with code which will work to remember what a user has selected on the next page?

    <link href="style2.css" type="text/css" rel="stylesheet" id="stylesheet" />
    <script language="javascript">
    <!--
    function changeStyle(name){
    if(name=='style1')
    document.getElementById('stylesheet').href='style1.css';
    else if(name=='style2')
    document.getElementById('stylesheet').href='style2.css';
    else if(name=='style3')
    document.getElementById('stylesheet').href='style3.css';}



    <td width="22"><input type="button" name="SmallA" class="SmallA" value="A" title="Small Font Size" onclick="changeStyle('style1')"></td>
    <td width="24"><input type="button" name="MediumA" class="MediumA" value="A" title="Medium Font Size" onclick="changeStyle('style2')"></td>
    <td width="26"><input type="button" name="LargeA" class="largeA" value="A" title="Large Font Size" onclick="changeStyle('style3')"></td>

  2. #2
    Join Date
    Oct 2006
    Posts
    939
    What you need is a cookie script?

  3. #3
    Join Date
    Jan 2011
    Location
    United Kingdom, London
    Posts
    117
    Code:
    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;
    }
    Last edited by maurycy; 06-27-2012 at 08:59 AM.
    Javascript, jQuery / Prototype & PHP expert ready to complete your work on Freelancer.co.uk - Hire Me at https://www.freelancer.co.uk/users/1...&action=hireme

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles