Saving dropdown menu selection in a cookie?

后端 未结 3 1627
轻奢々
轻奢々 2020-12-31 11:26

I\'ve seen this posted a few times, but I wasn\'t able to get the code working at all. I need help making this drop down menu save its settings to a cookie, so when the user

相关标签:
3条回答
  • 2020-12-31 12:02

    You should look into creating Session variables in PHP.

    http://www.w3schools.com/php/php_sessions.asp

    You can save variables to a session and when you load the page, you check the value of the variable you set, depending on it's value, the dropdown could behave a certain way. Maybe store the name of the dropdown item as a session variable?

    0 讨论(0)
  • 2020-12-31 12:06

    Something along these lines should work for you. The function to create a cookie via javascript was found in Setting a Cookie from JavaScript, a post on javascripter.net.

    HTML:

    <select id="ThemeSelect" onchange="setCookie('theme', this.value, 365);">
        <option value="zelda">Zelda</option>
        <option value="smb2">SMB 2</option>
    </select>
    

    Javascript:

    function setCookie(cookieName, cookieValue, nDays) {
        var today = new Date();
        var expire = new Date();
    
        if (!nDays) 
            nDays=1;
    
        expire.setTime(today.getTime() + 3600000*24*nDays);
        document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
    }
    

    Edit:

    Save the cookie

    I have merged the two functions into one for you.

    HTML:

    <select id="ThemeSelect" onchange="saveTheme(this.value);">
        <option value="zelda">Zelda</option>
        <option value="smb2">SMB 2</option>
    </select>
    

    Javascript:

    var saveclass = null;
    
    function saveTheme(cookieValue)
    {
        var sel = document.getElementById('ThemeSelect');
    
        saveclass = saveclass ? saveclass : document.body.className;
        document.body.className = saveclass + ' ' + sel.value;
    
        setCookie('theme', cookieValue, 365);
    }
    
    function setCookie(cookieName, cookieValue, nDays) {
        var today = new Date();
        var expire = new Date();
    
        if (nDays==null || nDays==0)
            nDays=1;
    
        expire.setTime(today.getTime() + 3600000*24*nDays);
        document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
    }
    

    Live DEMO

    Read the cookie on return to the page

    Thanks to dunsmoreb

    We can get the cookie using this function, shamelessly stolen from this question.

    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;
    }
    

    Then we need to select the value when the page loads. The following code will accomplish just that:

    document.addEventListener('DOMContentLoaded', function() {
        var themeSelect = document.getElementById('ThemeSelect');
        var selectedTheme = readCookie('theme');
    
        themeSelect.value = selectedTheme;
        saveclass = saveclass ? saveclass : document.body.className;
        document.body.className = saveclass + ' ' + selectedTheme;
    });
    

    Live DEMO

    0 讨论(0)
  • 2020-12-31 12:15

    Note: This answers adds to Josh Mein's answer. Feel free to merge it.

    We can get the cookie using this function, shamelessly stolen from this question.

    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;
    }
    

    Then we need to select the value when the page loads. The following code will accomplish just that.

    document.addEventListener('DOMContentLoaded', function() {
      var themeSelect = document.getElementById('ThemeSelect');
      var selectedTheme = readCookie('theme');
    
      if (selectedTheme) {
        themeSelect.value = selectedTheme;
      }
    });
    
    0 讨论(0)
提交回复
热议问题