使用javascript在下拉菜单中创建和读取cookie

时间:2013-01-15 14:43:06

标签: javascript

我正在使用此HTML代码:

<head>

<link href="defualt.css" rel="stylesheet" type="text/css" />
<link href="theme1.css" title="theme1" rel="alternate stylesheet" type="text/css" />
<link href="theme2.css" title="theme2" rel="alternate stylesheet" type="text/css" />
<link href="theme3.css" title="theme3" rel="alternate stylesheet" type="text/css"  />

<script type="text/javascript" src="javascript.js"></script>

</head>
<body>

<form>
    <select id="myList" >
        <option value="default">Default</option>
        <option value="theme1">Theme 1</option>
        <option value="theme2">Theme 2</option>  
        <option value="theme3">Theme 3</option>
    </select>
</form>

</body>

这是更改样式表的表单。这是我正在使用的javascript:

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  } 
  createCookie("style", title, 7);
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

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

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);


function initate()
{

document.getElementById("myList").onchange = function() {
   setActiveStyleSheet(this.value);
   return false
};

}

window.onload = initate;

样式表的更改效果很好,但我希望在重新加载页面后,菜单中选择的选项保持不变。这适用于Firefox,但不适用于Chrome。我对javascripts很新,我自己没有编写所有代码,所以我并不完全理解它。是否可以将所选菜单保存在同一个cookie中,或者更好/更容易创建一个新菜单?

1 个答案:

答案 0 :(得分:1)

如果您正在重新加载页面,则无法在页面(HTML,Javascript)本身中保留下拉选择的状态。我建议您使用cookie来存储当前选择,读取页面加载时的值,并相应地在下拉列表中设置选项。

对于cookie JS,请参阅:http://www.quirksmode.org/js/cookies.html

function initate() {
    window.selectBox = document.getElementById("myList");
    var cookie = readCookie("style");
    var title = cookie ? cookie : getPreferredStyleSheet();
    setActiveStyleSheet(title);
    getTheme();
    selectBox.onchange = function () {
        setActiveStyleSheet(this.value);
        setTheme();
    };
}
window.onload = initate;

function setActiveStyleSheet(title) {
    var i, a, main;
    for (i = 0; (a = document.getElementsByTagName("link")[i]) ; i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            a.disabled = true;
            if (a.getAttribute("title") == title) a.disabled = false;
        }
    }
    createCookie("style", title, 7);
}

function getActiveStyleSheet() {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]) ; i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
    }
}

function getPreferredStyleSheet() {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]) ; i++) {
        if (a.getAttribute("rel").indexOf("style") != -1
           && a.getAttribute("rel").indexOf("alt") == -1
           && a.getAttribute("title")
           ) return a.getAttribute("title");
    }
}

function getTheme() {
    var storedThemeVal = readCookie('selectedTheme');
    if (storedThemeVal != null && storedThemeVal != "") {
        for (var i = 0; i < selectBox.options.length; i++) {
            if (selectBox.options[i].value == storedThemeVal) {
                selectBox.selectedIndex = i;
            }
        }
    }
}

function setTheme() {
    var selectedThemeVal = selectBox.options[selectBox.selectedIndex].value;
    createCookie('selectedTheme', selectedThemeVal);
}

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

请注意,Chrome默认情况下不支持本地Cookie(file://协议)。要么在服务器环境中测试,要么通过将--enable-file-cookies添加到Chrome的快捷方式来启用文件Cookie(因此,如果您有快捷方式,请右键单击它,单击属性并编辑Target以包含--enable-file-cookies最后,在引号后面。)

或者,您也可以修改URL(用户查询字符串参数)来存储/读取当前选择(类似于上面的内容)。但是,如果用户离开网站并返回该网站,这当然会丢失。

Adding a parameter to the URL with JavaScript