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

javascript - A question about window.open and cookie - Stack Overflow

programmeradmin3浏览0评论

Say I have two apps, www.test and sub.test, now in sub.test, I create a window to load www.test with codes like :

window.open('www.test'); 

So the window just popup and load www.test successfully.
Then I set a cookie in sub.test, say "uname=wong2;domain=.test", I've learned that with set to domain=.test, all sites with domain test(such as www.test, aaa.test, test) can read the cookie.

But when I try to load the cookie from the window that just popup with www.test, it can't get it.

Then I found that if I don't use window.open but directly open www.test in browser, it works.

So is there some restrictions on window.open and cookie?

Say I have two apps, www.test. and sub.test., now in sub.test., I create a window to load www.test. with codes like :

window.open('www.test.'); 

So the window just popup and load www.test. successfully.
Then I set a cookie in sub.test., say "uname=wong2;domain=.test.", I've learned that with set to domain=.test., all sites with domain test.(such as www.test., aaa.test., test.) can read the cookie.

But when I try to load the cookie from the window that just popup with www.test., it can't get it.

Then I found that if I don't use window.open but directly open www.test. in browser, it works.

So is there some restrictions on window.open and cookie?

Share Improve this question asked Apr 18, 2011 at 12:27 wong2wong2 35.8k51 gold badges137 silver badges182 bronze badges 2
  • Not that I have ever heard of. Did you set the path to "/" too? And are you using the same port numbers and protocol (http/https) – mplungjan Commented Apr 18, 2011 at 12:29
  • In which browser(s) does this problem occur? – JAAulde Commented Apr 18, 2011 at 12:35
Add a ment  | 

2 Answers 2

Reset to default 2

just check how you set the cookie:

var domain = 'test.';
var expires = (function(days){
    date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    return date.toUTCString();
})(5);
var name = 'myCookie';
var path = '/';
var value = 'foo';    

document.cookie = name + "=" + encodeURIComponent(value) + "; expires=" + expires + "; path='" + path + "'; domain=" + domain + ";"; 

That is called cross domain and you cant set cookie in one domain and try to access that in different domain. Browsers wont allow doing this.I think you can acplish this using iframe or same origin policy or try using document.domain I am not sure what you want to do exactly.

发布评论

评论列表(0)

  1. 暂无评论