PDA

View Full Version : Help with Javascript Cookies - Table Width



theflyingminstrel
09-28-2008, 04:24 PM
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>
<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>

jscheuer1
09-28-2008, 06:28 PM
<!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>

theflyingminstrel
09-29-2008, 05:19 AM
Thanks a million dude, worked perfect!