简体   繁体   English

JavaScript的cookie的工作不适合域

[英]Javascript Cookie Function not working for Domain

Here are the functions Im using: 这是Im使用的功能:

Set Cookie: 设置Cookie:

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );

  if ( secure )
        cookie_string += "; secure";

  document.cookie = cookie_string;
}

Read Cookie: 读取Cookie:

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

Delete Cookie: 删除Cookie:

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

The Jquery I use to construct the cookie: 我用来构造Cookie的Jquery:

    if(get_cookie('visible')== 'no') {
        $("#wrapper").hide();
        $(".help").hide();
        $("#slid .show").show();
        $("#slid .hide").hide();
        } else {
            $("#slid .show").hide();
            $("#slid .hide").show();
        }
    $("#slider").click(function() {
        if(get_cookie('visible')== null) {
            set_cookie('visible','no', 2020, 01,01, '/', 'domain.com');
        } else {
            delete_cookie('visible');
        }
            $(".help").slideToggle();
                $("#wrapper").animate({ opacity: 1.0 },200).slideToggle(200, function() {
                    $("#slid img").toggle();
                });
    });

Im trying to set the cookie for all pages that exist under domain.com with the path '/'. 我试图为domain.com下存在的所有带有路径“ /”的页面设置cookie。

However using these functions and jQuery it doesn't appear to be working, any anyone give me an idea of where im going wrong? 但是,使用这些功能和jQuery似乎无法正常工作,任何人都可以让我知道我哪里出错了吗?

Works for me, as long as the domain is matching or omitted, and there's nothing in domain or path that gets mangled by escape() . 只要domain匹配或省略,对我都有效,并且domainpath中没有任何东西被escape()破坏。

(You shouldn't use escape() for the cookie parameters: there is no standard escaping scheme for these. It's generally a bad idea to use escape() / unescape() for anything ; it's a non-standard encoding scheme that is not the same as URL-encoding, so if you try to read the cookie from some other language like PHP you won't be able to decode it properly using any built-in function. If you want real URL-encoding for the cookie values themselves, go for encodeURIComponent / decodeURIComponent , though again, these are still not used for cookie parameters.) (您不应该对cookie参数使用escape()对于这些参数没有标准的转义方案。对于任何东西都使用escape() / unescape()通常是一个坏主意;这是一种非标准的编码方案,并非与URL编码相同,因此,如果您尝试从其他语言(如PHP)读取cookie,则将无法使用任何内置函数对其进行正确解码。如果您想对cookie值本身进行真正的URL编码,继续使用encodeURIComponent / decodeURIComponent ,尽管如此,它们仍然不用于Cookie参数。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM