使用Javascript变量设置Cookie域

CLi*_*own 1 javascript jquery

我使用jQuery从页面的HTML中获取域:

domainUrl = $("p.domain").text();
Run Code Online (Sandbox Code Playgroud)

出于测试目的:

<p class="domain">.vl3.co.uk</p>
Run Code Online (Sandbox Code Playgroud)

这也是Im测试脚本所在的域。

然后,这会给出包含正确域的警报:

alert(domainUrl);
Run Code Online (Sandbox Code Playgroud)

我想使用该变量在Cookie中设置域:

set_cookie('visible', 'no', 2020, 1, 1, '/', '+domainUrl+');
Run Code Online (Sandbox Code Playgroud)

这是set 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;
}
Run Code Online (Sandbox Code Playgroud)

为什么没有设置cookie域?

我认为问题是在设置Cookie时如何使用domainUrl变量?

net*_*tos 5

应该是:set_cookie('visible','no',2020,1,1,'/',domainUrl);

请尝试此扩展,它可以正常工作,它包含以下所有内容:

http://plugins.jquery.com/project/Cookie

然后,您只需要编写:

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com' });
Run Code Online (Sandbox Code Playgroud)