为什么cookie代码不起作用?
我试图让那些需要更好的色彩对比度的人访问我的网站,允许他们选择他们的css表,这将在我的网站上使用.我从网上下载了代码(2010年编码?),代码在主页上可以工作,但Cookie代码本身不会将用户的选择应用于任何其他页面.
更新:我需要把Cookie代码放在所有页面上吗?我觉得我没必要这么做.谢谢!我确实注意到,当我回到主页时,我的选择被保留了下来.那么……如何让Cookie应用于所有页面?谢谢!
注意:我确实有Facebook和Google的代码来"喜欢我".此外,我还有谷歌主干代码,可以让我的HTML5和CSS3在还不支持HTML5的浏览器上运行.
此外,我确实在所有页面上都贴上了新的Body标签.我从主页上复制了它,以确保我没有出错.
最后,在我的vegan.html页面上,我确实添加了指向可供选择的css表的链接,但我没有在其他非主页上这样做.我以为这可能会解决问题,但事实并非如此.
选择 | 换行 | 行号
- <!-- BEGIN ABILITY -->
- <link rel="stylesheet" title="av" type="text/css" href="http://www.allergicvegetarian.com/css/ability/av.css" />
- <link rel="alternate stylesheet" title="btwb" type="text/css" href="http://www.allergicvegetarian.com/css/ability/btwb.css" />
- <link rel="alternate stylesheet" title="btyb" type="text/css" href="http://www.allergicvegetarian.com/css/ability/btyb.css" />
- <script type="text/javascript">
- var style_cookie_name = "alergikvegtarian" ;
- var style_cookie_duration = 365 ;
- function switch_style ( css_title )
- {
- // You may use this script on your site free of charge provided
- // you do not remove this notice or the URL below. Script from
- // http://www.thesitewizard.com/javascripts/change-style-sheets.shtml
- var i, link_tag ;
- for (i = 0, link_tag = document.getElementsByTagName("link") ;
- i < link_tag.length ; i++ ) {
- if ((link_tag[i].rel.indexOf( "stylesheet" ) != -1) &&
- link_tag[i].title) {
- link_tag[i].disabled = true ;
- if (link_tag[i].title == css_title) {
- link_tag[i].disabled = false ;
- }
- }
- set_cookie( style_cookie_name, css_title,
- style_cookie_duration );
- }
- }
- function set_style_from_cookie()
- {
- var css_title = get_cookie( style_cookie_name );
- if (css_title.length) {
- switch_style( css_title );
- }
- }
- function set_cookie ( cookie_name, cookie_value,
- lifespan_in_days, valid_domain )
- {
- // copywrite script per above do not separate
- var domain_string = valid_domain ?
- ("; domain=" + valid_domain) : '' ;
- document.cookie = cookie_name +
- "=" + encodeURIComponent( cookie_value ) +
- "; max-age=" + 60 * 60 *
- 24 * lifespan_in_days +
- "; path=/" + domain_string ;
- }
- function get_cookie ( cookie_name )
- {
- // copywrite script per above do not separate
- var cookie_string = document.cookie ;
- if (cookie_string.length != 0) {
- var cookie_value = cookie_string.match (
- '(^|;)[\s]*' +
- cookie_name +
- '=([^;]*)' );
- return decodeURIComponent ( cookie_value[2] ) ;
- }
- return '' ;
- }
- </script>
选择 | 换行 | 行号
- <body onload="set_style_from_cookie()">
有效的表单:
选择 | 换行 | 行号
- <form>
- <input type="submit"
- onclick="switch_style('av');return false;"
- name="theme" value="Purple Background" id="av">
- <input type="submit"
- onclick="switch_style('btwb');return false;"
- name="theme" value="Black Text on White" id="btwb">
- <input type="submit"
- onclick="switch_style('btyb');return false;"
- name="theme" value="Black Text on Yellow" id="btyb">
- </form>
链接到网站
选择Style,然后点击"素食",你就会明白我的意思.
谢谢!