我下载的样式表切换器似乎无法正常工作。
切换器在加载新样式表时工作,但是它打算存储和读取的 cookie 从未真正发生(我假设当我刷新页面时它会恢复为默认样式表)。
chrome中的控制台返回以下错误:
未捕获的 ReferenceError:未定义 readCookie
现在我的理论是 readCookie 变量没有定义,我应该把它移到顶部,但这只是使脚本完全不活动。
当谈到 jQuery 时,我不是一个完整的新手,但这是一个下载的脚本,我以前没有使用 javascript cookie 的经验。
任何帮助将不胜感激。
(function($)
{
$(document).ready(function() {
$('.styleswitch').click(function()
{
switchStylestyle(this.getAttribute("rel"));
return false;
});
var c = readCookie('style');
if (c) switchStylestyle(c);
});
function switchStylestyle(styleName)
{
$('link[@rel*=style][title]').each(function(i)
{
this.disabled = true;
if (this.getAttribute('title') == styleName) this.disabled = false;
});
createCookie('style', styleName, 365);
}
})(jQuery);
// cookie functions http://www.quirksmode.org/js/cookies.html
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 var 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;
}
function eraseCookie(name)
{
createCookie(name,"",-1);
}