阅读cookie工作在Firefox/IE,不是Chrome
Reading cookies working in Firefox/IE, NOT Chrome
我有一个代码,可以改变样式表,并将它们保存在Firefox和IE的cookie中,但它在Chrome中不起作用。
这是我使用的代码:
<head>
<link href="default.css" title="default" rel="stylesheet" type="text/css" />
<link href="theme1.css" title="theme1" rel="stylesheet" type="text/css" />
<link href="theme2.css" title="theme2" rel="stylesheet" type="text/css" />
<link href="theme3.css" title="theme3" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="javascript.js"></script>
</head>
<body>
<ul id="dropdown">
<li> Choose theme
<ul>
<li id="stylesheet1" > <a href="#"> Default </a></li>
<li id="stylesheet2" > <a href="#"> Theme 1 </a></li>
<li id="stylesheet3" > <a href="#"> Theme 2 </a></li>
<li id="stylesheet4" > <a href="#"> Theme 3 </a></li>
</ul>
</li>
</ul>
</body>
javascript.js包含这个:
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;
}
}
}
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);
}
window.onunload = function(e) {
var title = getActiveStyleSheet();
createCookie("style", title, 365);
}
var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
function initate()
{
document.getElementById("stylesheet1").onclick = function() {
setActiveStyleSheet("default");
return false
};
document.getElementById("stylesheet2").onclick = function() {
setActiveStyleSheet("theme1");
return false
};
document.getElementById("stylesheet3").onclick = function() {
setActiveStyleSheet("theme2");
return false
};
document.getElementById("stylesheet4").onclick = function() {
setActiveStyleSheet("theme3");
return false
}
}
window.onload = initate;
它确实可以在Chrome中更改样式表,但当我重新加载页面时,它不会像在IE和Firefox中那样保留所选择的样式表。我不明白为什么它不能在Chrome中工作。我是javascript的新手,使用的大部分代码不是我写的,而是来自不同的地方,所以我不太理解大部分。
你重写了之前的onclick处理程序,只有第二个正在执行:
document.getElementById("stylesheet1").onclick = function() {
createCookie(T1, style, 7);
};
...
var style1 = document.getElementById("stylesheet1");
style1.onclick = function () {
setActiveStyleSheet("default");
return false;
};
把我关于使用jQuery的评论放到一个角度来看,你的整个initiate
函数,目前大约50行,可以这样重写,大约7行:
$("li a").click(function () {
# Turn our text ("Theme 1") into the name of the sheet ("theme1")
var sheetName = $(this).text().toLowerCase().replace(/ /g, "");
createCookie(T1, sheetName, 7)
setActiveStyleSheet(sheetName);
});
相关文章:
- 后焦点更改为IE 11中的地址栏,而不是转移到表单中的下一个控件
- 在FF和IE中使用vimeo播放器的问题-加载flash播放器而不是使用HTML5播放器
- window.打开IE 11中的新选项卡而不是新窗口
- 在Chrome中点击两次,而不是IE或Firefox
- VBA IE单击/显示没有id或标记而不是超链接的javascript
- 锚标记重新加载页面,而不是在IE中处理onclick
- IE 0.width为null或不是对象
- ActiveXObject 不是为除 IE 之外的所有浏览器定义的
- TypeError:undefined 不是对象 - Safari 和 IE
- java脚本退格键和删除在IE中工作,而不是在FireFox中工作
- Selenium IE Driver 打开新窗口,而不是 window.open() 上的新选项卡
- Cookie不是在Safari,ios中设置的,而是在ie,ff,chrome中工作
- Javascript 对象在 IE 中为 null 或不是 object
- Javascript适用于Firefox而不是IE.为什么
- 如何使样式元素的一部分成为函数?(IE:颜色:$random而不是颜色:红色)
- 受信任站点 WinXP IE 8 的错误“window.opener.top 为空或不是对象”
- 为什么 window.location 追加而不是替换 IE 中的 URL
- JavaScript手风琴菜单工作在IE不是chrome或Firefox
- Chrome执行代码两次,而IE不是
- Javascript代码动态改变图像的工作在IE不是FF, Chrome等:(