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

javascript - Setting a Site Wide Cookie - Stack Overflow

programmeradmin4浏览0评论

Here's what I hope is a quick question...

I am trying to set a cookie that can be used sitewide. I'm creating a lead generation type site. I want users to fill out a form in order to access exclusive information. Once they fill out the form, they have access to the info.

I am dropping a cookie when they user submits the form so that they can just get straight to the content the next time they visit the site. The form they fill out is in the sidebar of every page on the site. When the user fills out the form on one page, they shouldn't see it on ANY page of the site.

Everything is working, except for the sitewide bit. I think the issue is in this bit of code:

function set_cookie(name, value, expires, path, domain, secure){
if (!expires){expires = new Date()}
document.cookie = name + "=" + escape(value) + 
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure");
}

But here's the full code below. THANKS SO MUCH!

<script type="text/javascript">
<!--
cookie_name="landasp"
expdays=365

// An adaptation of Dorcht's cookie functions.

function set_cookie(name, value, expires, path, domain, secure){
if (!expires){expires = new Date()}
document.cookie = name + "=" + escape(value) + 
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure");
}

function get_cookie(name) {
var arg = name + "=";
var alen = arg.length; 
var clen = document.cookie.length;
var i = 0; 
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg){
return get_cookie_val(j); 
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function get_cookie_val(offset){
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function delete_cookie(name,path,domain){
document.cookie = name + "=" +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-00 00:00:01 GMT";
}

function saving_cookie(){
var expdate = new Date ();
expdate.setTime (expdate.getTime() + (expdays*24*60*60*1000*30)); //set for one month
Data="cooked"

set_cookie(cookie_name,Data,expdate)
}

function get_cookie_data(){
inf=get_cookie(cookie_name)
if(!inf){
document.getElementById("display1").style.display="block"
}
else{
document.getElementById("display2").style.display="block"
}
}

// -->
</script> 

Here's what I hope is a quick question...

I am trying to set a cookie that can be used sitewide. I'm creating a lead generation type site. I want users to fill out a form in order to access exclusive information. Once they fill out the form, they have access to the info.

I am dropping a cookie when they user submits the form so that they can just get straight to the content the next time they visit the site. The form they fill out is in the sidebar of every page on the site. When the user fills out the form on one page, they shouldn't see it on ANY page of the site.

Everything is working, except for the sitewide bit. I think the issue is in this bit of code:

function set_cookie(name, value, expires, path, domain, secure){
if (!expires){expires = new Date()}
document.cookie = name + "=" + escape(value) + 
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure");
}

But here's the full code below. THANKS SO MUCH!

<script type="text/javascript">
<!--
cookie_name="landasp"
expdays=365

// An adaptation of Dorcht's cookie functions.

function set_cookie(name, value, expires, path, domain, secure){
if (!expires){expires = new Date()}
document.cookie = name + "=" + escape(value) + 
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure");
}

function get_cookie(name) {
var arg = name + "=";
var alen = arg.length; 
var clen = document.cookie.length;
var i = 0; 
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg){
return get_cookie_val(j); 
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function get_cookie_val(offset){
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function delete_cookie(name,path,domain){
document.cookie = name + "=" +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-00 00:00:01 GMT";
}

function saving_cookie(){
var expdate = new Date ();
expdate.setTime (expdate.getTime() + (expdays*24*60*60*1000*30)); //set for one month
Data="cooked"

set_cookie(cookie_name,Data,expdate)
}

function get_cookie_data(){
inf=get_cookie(cookie_name)
if(!inf){
document.getElementById("display1").style.display="block"
}
else{
document.getElementById("display2").style.display="block"
}
}

// -->
</script> 
Share Improve this question asked Jan 26, 2012 at 13:52 AshAsh 3851 gold badge5 silver badges16 bronze badges 1
  • wele to stackoverflow! If you feel an answer answers your question please mark it as answered by clicking the green checkbox. – vidstige Commented Jan 26, 2012 at 14:11
Add a ment  | 

1 Answer 1

Reset to default 16

You should specify a site wide path, if the path is not given:

((path == null) ? "; path=/" : "; path=" + path) +

You can debug the cookies using Firebug, just have a look at the set cookies.

发布评论

评论列表(0)

  1. 暂无评论