...

View Full Version : :: Saving a dropdown list to a cookie??



MCBowler
10-24-2005, 05:32 PM
I have dropdown list like this:
<select id="myList">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>

and i modify it using javascript and once i have finished modifying the dropdown list i want to save its current state to a cookie so that when i reload the page the last state of the dropdown list is retrieved from the cookie and written into the page is this possible??

Help me please

Lerura
10-24-2005, 06:30 PM
<head><script><!--
function SETcookie(){
document.cookie="Selected="+document.getElementById('myList').selectedIndex;
}
function GETcookie(){
if (document.cookie){
eval(document.cookie);
document.getElementById('myList').selectedIndex=Selected;
}}// --></script></head>
<body onLoad="GETcookie()">
<select id="myList" onChange="SETcookie()">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>

MCBowler
10-24-2005, 07:18 PM
Thanks for that mate but that remembers what was last selected which i suppose is part of the dropdown list state so in effect its what i have asked so i will make myself clearer.. Here goes lol.

i have this code to add and remove options in the dropdown list:


<html>
<head>
<script>
function Add_Item() {
var oOption = document.createElement("OPTION");
oSelect.options.add(oOption);
oOption.innerText = document.getElementById('optinnrtxt').value
oOption.value = document.getElementById('optvl').value
}

function Remove_Item()
{
var Rem = document.getElementById('oSelect');
var i;
for (i = Rem.length - 1; i>=0; i--) {
if (Rem.options[i].selected) {
Rem.remove(i);
}
}
}
</script>
</head>
<body>
<select ID="oSelect" size=5 multiple style="width: 200px;">
<option VALUE="1">Item One</option>
</select>
<br/>
<a href="javascript:Add_Item();">Add to list</a><br/>
Label: <input type="text" ID="optinnrtxt" size="20"><br/>
Value: <input type="text" ID="optvl" size="20"><br/>
<a href="javascript:Remove_Item();">Remove selected items from list</a>
</body>
</html>And once i have used that code to add and remove options as desired etc. i then want to save the dropdown list and all its current options to a cookie so that when i reload the page i get the same set of options and i can modify as i want overwrite the cookie and so on.

Its for personal use and its an idea for an organiser so i don't forget doctors appointments and stuff like that.

oh yeah i want the cookie to be valid for a year so that it isn't expired when i load up the page a week later or what not.

MCBowler
10-25-2005, 01:02 PM
Well ok if no-one here knows anything about advanced cookies then i will continue searching the internet. But i think i have found code so that i can save a cookie for each option but its not prefered because of when i try to flush out junk cookies.

But still any help will be greatly appreciated so if anyone knows anything about advanced cookies and is kind enough to share there knowlesge with this lively online community then please do so. I would think that what i have asked could be similar to storing user preferences but i dunno i'm still basically a starter though when i find a script online i tend to mess about with it extensively to get different results and its teaching me a lot.

Lerura
10-25-2005, 09:24 PM
add these function to the head:

function Restore_Item(IT,VA) {
var oOption = document.createElement("OPTION");
document.getElementById('oSelect').options.add(oOption);
oOption.innerText = IT;
oOption.value = VA;
}
function Clear_Items(){
var Rem = document.getElementById('oSelect');
var i;
while (Rem.length>0){
Rem.remove(0);
}
}
function Save_Items(){
SaveString='SelectItems="';
var Rem = document.getElementById('oSelect');
var i;
for (i = 0; i<Rem.length ; i++) {
SaveString+=Rem.options[i].innerText+','+Rem.options[i].value+',';
}
SaveString=SaveString.substring(0,SaveString.length-1);
SaveString+='"';
document.cookie=SaveString;
}
function Restore_Select(){
eval(document.cookie);
Data=SelectItems.split(',');
for (x=0;x<Data.length-1;x+=2){
Restore_Item(Data[x],Data[x+1]);
}
}
function Restore(){
if (document.cookie){
Clear_Items();
Restore_Select();
}
}



add this link to your body:
<a href="javascript:Save_Items();">Save List</a> and add this to your bodytag:

onload="Restore()"

when it comes to expiration: (as far as I remember)
offline-cookies will be deleted when exiting the browser since they only exist in RAM.

online-cookies will not expire unless you have set an expiration-date;

vwphillips
10-25-2005, 09:27 PM
see

http://www.vicsjavascripts.org.uk/FormCompendium/FormCompendium.htm#f19

MCBowler
10-26-2005, 02:37 PM
Are you sure that theres no way of making offline cookies permanent?

vwphillips
10-26-2005, 03:51 PM
my offline cookies observe the expriery date

have a look in your cookie folder C:\WINDOWS\Cookies

MCBowler
10-26-2005, 06:18 PM
C:\Windows\Cookies doesn't exist on Windows Xp but i already know where my Cookies are stored anyway thanks for your help vwphillips and lerura :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum