DPCT
asked on
Cannot set cookie value when path is set
I want to have the user click a button on a page in SharePoint that will upload a file then pre-populate a required field for that document library. I need to do all this client side. (no custom code allowed on the server). Since I can't modify the upload form, I thought I would try saving the value in a cookie from my first form, then read it from the edit form for the document library. My problem is that if I write the cookie using javascript
document.cookie = "MyField=MyValue";
it saves the cookie. If I instead use
document.cookie = "MyField=MyValue; path=/";
it fails to save the cookie.
When I save it without the path, the form that saved the cookie can read the value back, but other forms, even in the same folder on the same site, can't see the cookie value and return null.
Any experts out there have any experiance with SP cookie madness?
document.cookie = "MyField=MyValue";
it saves the cookie. If I instead use
document.cookie = "MyField=MyValue; path=/";
it fails to save the cookie.
When I save it without the path, the form that saved the cookie can read the value back, but other forms, even in the same folder on the same site, can't see the cookie value and return null.
Any experts out there have any experiance with SP cookie madness?
ASKER
I tried this. It is now setting the cookie, but I still can't read it from another page. only the page that set the cookie.
Same hostname?
Use this
<script src="cookie.js"></script>
<script>
setCookie("MyField","MyVal ue",expiry Date,"/");
</script>
<script src="cookie.js"></script>
<script>
setCookie("MyField","MyVal
</script>
// cookie.js file
var daysToKeep = 14; // default cookie life...
var today = new Date();
var expiryDate = new Date(today.getTime() + (daysToKeep * 86400000));
/* Cookie functions originally by Bill Dortsch */
function setCookie (name,value,expires,path,theDomain,secure) {
value = escape(value);
var theCookie = name + "=" + value +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((theDomain) ? "; domain=" + theDomain : "") +
((secure) ? "; secure" : "");
document.cookie = theCookie;
}
function getCookie(Name) {
var search = Name + "="
if (document.cookie.length > 0) { // if there are any cookies
var offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
var end = document.cookie.indexOf(";", offset)
// set index of end of cookie value
if (end == -1) end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
}
}
function delCookie(name,path,domain) {
if (getCookie(name)) document.cookie = name + "=" +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
";expires=Thu, 01-Jan-70 00:00:01 GMT";
}
ASKER
Yes. The same host name. I can't read a cookie set by one page from another page on the same server with the same path except for the file name.
I have tried a number of these code solutions. I Including this one. I believe my issue has something to do with the site being in SharePoint. Even with the path set to "/", I cannot read the set cookie from any other page than the one that wrote it. Sorry if I'm not being too clear. Has anyone used cookies on a SharePoint site across pages?
I have tried a number of these code solutions. I Including this one. I believe my issue has something to do with the site being in SharePoint. Even with the path set to "/", I cannot read the set cookie from any other page than the one that wrote it. Sorry if I'm not being too clear. Has anyone used cookies on a SharePoint site across pages?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
document.cookie = "MyField=MyValue";
Work :
document.cookie = "path=/";
Don't work :
document.cookie = "MyField=MyValue; path=/";
Work :
Open in new window