为什么cookie代码不起作用?

我试图让那些需要更好的色彩对比度的人访问我的网站,允许他们选择他们的css表,这将在我的网站上使用.我从网上下载了代码(2010年编码?),代码在主页上可以工作,但Cookie代码本身不会将用户的选择应用于任何其他页面.
更新:我需要把Cookie代码放在所有页面上吗?我觉得我没必要这么做.谢谢!我确实注意到,当我回到主页时,我的选择被保留了下来.那么……如何让Cookie应用于所有页面?谢谢!
注意:我确实有Facebook和Google的代码来"喜欢我".此外,我还有谷歌主干代码,可以让我的HTML5和CSS3在还不支持HTML5的浏览器上运行.
此外,我确实在所有页面上都贴上了新的Body标签.我从主页上复制了它,以确保我没有出错.
最后,在我的vegan.html页面上,我确实添加了指向可供选择的css表的链接,但我没有在其他非主页上这样做.我以为这可能会解决问题,但事实并非如此.

选择 | 换行 | 行号
  1. <!-- BEGIN ABILITY -->
  2.  
  3. <link rel="stylesheet" title="av" type="text/css" href="http://www.allergicvegetarian.com/css/ability/av.css" />
  4. <link rel="alternate stylesheet" title="btwb" type="text/css" href="http://www.allergicvegetarian.com/css/ability/btwb.css" />
  5. <link rel="alternate stylesheet" title="btyb" type="text/css" href="http://www.allergicvegetarian.com/css/ability/btyb.css" />
  6.  
  7. <script type="text/javascript">
  8.  
  9. var style_cookie_name = "alergikvegtarian" ;
  10. var style_cookie_duration = 365 ;
  11.  
  12. function switch_style ( css_title )
  13. {
  14. // You may use this script on your site free of charge provided
  15. // you do not remove this notice or the URL below. Script from
  16. // http://www.thesitewizard.com/javascripts/change-style-sheets.shtml
  17.   var i, link_tag ;
  18.   for (i = 0, link_tag = document.getElementsByTagName("link") ;
  19.     i < link_tag.length ; i++ ) {
  20.     if ((link_tag[i].rel.indexOf( "stylesheet" ) != -1) &&
  21.       link_tag[i].title) {
  22.       link_tag[i].disabled = true ;
  23.       if (link_tag[i].title == css_title) {
  24.         link_tag[i].disabled = false ;
  25.       }
  26.     }
  27.     set_cookie( style_cookie_name, css_title,
  28.       style_cookie_duration );
  29.   }
  30. }
  31.  
  32. function set_style_from_cookie()
  33. {
  34.   var css_title = get_cookie( style_cookie_name );
  35.   if (css_title.length) {
  36.     switch_style( css_title );
  37.   }
  38. }
  39.  
  40.  
  41. function set_cookie ( cookie_name, cookie_value,
  42.     lifespan_in_days, valid_domain )
  43. {
  44.     // copywrite script per above do not separate
  45.     var domain_string = valid_domain ?
  46.                        ("; domain=" + valid_domain) : '' ;
  47.     document.cookie = cookie_name +
  48.                        "=" + encodeURIComponent( cookie_value ) +
  49.                        "; max-age=" + 60 * 60 *
  50.                        24 * lifespan_in_days +
  51.                        "; path=/" + domain_string ;
  52. }
  53.  
  54.  
  55. function get_cookie ( cookie_name )
  56. {
  57.     // copywrite script per above do not separate
  58.     var cookie_string = document.cookie ;
  59.     if (cookie_string.length != 0) {
  60.         var cookie_value = cookie_string.match (
  61.                         '(^|;)[\s]*' +
  62.                         cookie_name +
  63.                         '=([^;]*)' );
  64.         return decodeURIComponent ( cookie_value[2] ) ;
  65.     }
  66.     return '' ;
  67. }
  68.  
  69.  
  70. </script>
选择 | 换行 | 行号
  1. <body onload="set_style_from_cookie()">
  2.  

有效的表单:

选择 | 换行 | 行号
  1. <form>
  2. <input type="submit"
  3.   onclick="switch_style('av');return false;"
  4.   name="theme" value="Purple Background" id="av">
  5.  
  6. <input type="submit"
  7.   onclick="switch_style('btwb');return false;"
  8.   name="theme" value="Black Text on White" id="btwb"> 
  9.  
  10. <input type="submit"
  11.   onclick="switch_style('btyb');return false;"
  12.   name="theme" value="Black Text on Yellow" id="btyb">
  13. </form>
  14.  

链接到网站
选择Style,然后点击"素食",你就会明白我的意思.
谢谢!

# 回答1


答:我必须在我的网站的所有页面上有完整的脚本代码.或者有JS的链接.

标签: Javascript

添加新评论