Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question :: Saving a dropdown list to a cookie??

    I have dropdown list like this:
    Code:
    <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

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    Code:
    <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>

  • #3
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    <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.

  • #4
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    add these function to the head:
    Code:
    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:
    Code:
    <a href="javascript:Save_Items();">Save List</a>
    and add this to your bodytag:
    Code:
    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;

  • #6
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,521
    Thanks
    3
    Thanked 507 Times in 494 Posts

  • #7
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you sure that theres no way of making offline cookies permanent?

  • #8
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,521
    Thanks
    3
    Thanked 507 Times in 494 Posts
    my offline cookies observe the expriery date

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

  • #9
    New to the CF scene
    Join Date
    Oct 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by MCBowler; 10-26-2005 at 06:35 PM.


  •  

    Posting Permissions

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