最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Set cookie domain with Javascript variable - Stack Overflow

programmeradmin2浏览0评论

Im taking the domain from the HTML of the page using jQuery:

domainUrl = $("p.domain").text();

for the purposes of testing:

<p class="domain">.vl3.co.uk</p>

Which is also the domain Im testing the script on.

This then give an alert containing the correct domain:

alert(domainUrl);

I want to the use that variable to set the domain in a cookie:

set_cookie('visible', 'no', 2020, 1, 1, '/', '+domainUrl+');

Here is the set cookie function:

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;
}

Why doesnt the cookie domain get set?

I think the problem is how im using the domainUrl variable when setting the cookie?

Im taking the domain from the HTML of the page using jQuery:

domainUrl = $("p.domain").text();

for the purposes of testing:

<p class="domain">.vl3.co.uk</p>

Which is also the domain Im testing the script on.

This then give an alert containing the correct domain:

alert(domainUrl);

I want to the use that variable to set the domain in a cookie:

set_cookie('visible', 'no', 2020, 1, 1, '/', '+domainUrl+');

Here is the set cookie function:

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;
}

Why doesnt the cookie domain get set?

I think the problem is how im using the domainUrl variable when setting the cookie?

Share Improve this question asked Oct 15, 2010 at 11:03 CLiownCLiown 13.9k50 gold badges127 silver badges205 bronze badges 4
  • What does domainURL contain? – Pekka Commented Oct 15, 2010 at 11:05
  • domainUrl contains .vl3.co.uk – CLiown Commented Oct 15, 2010 at 11:08
  • 2 and whats the domain where this script is running at? – Dr.Molle Commented Oct 15, 2010 at 11:47
  • You can just use ;domain and it will set the CURRENT domain – Piotr Kula Commented Jun 1, 2012 at 15:08
Add a ment  | 

2 Answers 2

Reset to default 5

It should be: set_cookie('visible', 'no', 2020, 1, 1, '/', domainUrl);

Pls, try this extension, it works, it prises all that:

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

Then you only have to write:

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.' });

Use jquery Annex library

I think this is the best way to get and set cookies using jQuery:

// cookie [writes and reads cookies]

//set cookie
$.cookie('kittencookie', 'fluffy', {expires : 7});

//get cookie
var kittenCookieValue = $.cookie('kittencookie');

For more details see documentation.

发布评论

评论列表(0)

  1. 暂无评论