Results 1 to 3 of 3

Thread: Help with Javascript Cookies - Table Width

  1. #1
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Javascript Cookies - Table Width

    Hi I was wondering if someone could help me code this so that this table stays the width chosen on refresh.

    Thanks so much!


    HTML Code:
    <html>
    <head>
    <script type="text/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 switchheight()
    {
    var orig='500';
    var el=document.getElementById('width');
    el.width=(el.width==='500')?'200':'500';
    }
    </script>
    </head>
    
    <body>
    <table class="orig" id="width">
    <tr>
    <td bgcolor="green">demo table</td>
    </tr>
    </table>
    
    <input type="button" value="width" onclick="switchheight();Cookie.setCookie('tableClass','class');">
    
    				
    
    
    <script type="text/javascript">
    
    window.onload=function () {
        var lngt = document.getElementsById('class');
        if(readCookie('width')) {
            lngt[0].el.width=readCookie('width');
            }
    };
    
    </script>
    
    </body>
    </html> 

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Remember Width - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    #width td {
    background-color: #5f5;
    padding-bottom:1ex;
    }
    </style>
    <script type="text/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 switchheight(){
    var el=document.getElementById('width');
    el.width=(el.width==='500')?'200':'500';
    createCookie('width', el.width);
    };
    
    window.onload = function () {
        if(readCookie('width'))
            document.getElementById('width').width=readCookie('width');
    };
    
    </script>
    </head>
    
    <body>
    <table id="width">
    <tr>
    <td>demo table</td>
    </tr>
    </table>
    
    <div>
    <input type="button" value="width" onclick="switchheight();">
    </div>
    
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks a million dude, worked perfect!

Bookmarks

Posting Permissions

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