Javascript and cookies

  • Thread starter Thread starter Wardeaux
  • Start date Start date
W

Wardeaux

I set a cookie in my code behind:

Dim cookieFocus As New HttpCookie("PMFocus")
cookieFocus.Value = "D"

Response.Cookies.Add(cookieFocus)

I read the cookie in my html page:
function doFocus(str)
{ var allcookies = document.cookie;
var pos = allcookies.indexOf("PMFocus=");
var mydate = new Date();
mydate.setFullYear(2000);
alert(allcookies);
if (pos != -1)
{
var start = pos + 8;
var end = allcookies.indexOf(";",start);
if (end == -1) end = allcookies.length;
var Myvalue = allcookies.substring(start,end);
alert(Myvalue);
if (Myvalue == "D")
{
alert("Found Cookie");
document.all(str).focus();
}
}
document.cookie = "PMFocus=E"
alert("here");
}

When I try to set the cookie to a new value it just creates new cookie by
the same name...
and when I change the value from my codebehind, it doesn't change the one
created by my clientside javascript
Any clues how to get them to work on the same cookie?

MTIA
wardeaux
 
Wardeaux said:
I set a cookie in my code behind:

Dim cookieFocus As New HttpCookie("PMFocus")
cookieFocus.Value = "D"

Response.Cookies.Add(cookieFocus)

I read the cookie in my html page:
function doFocus(str)
{ var allcookies = document.cookie;
var pos = allcookies.indexOf("PMFocus=");
var mydate = new Date();
mydate.setFullYear(2000);
alert(allcookies);
if (pos != -1)
{
var start = pos + 8;
var end = allcookies.indexOf(";",start);
if (end == -1) end = allcookies.length;
var Myvalue = allcookies.substring(start,end);
alert(Myvalue);
if (Myvalue == "D")
{
alert("Found Cookie");
document.all(str).focus();
}
}
document.cookie = "PMFocus=E"
alert("here");
}

When I try to set the cookie to a new value it just creates new cookie by
the same name...
and when I change the value from my codebehind, it doesn't change the one
created by my clientside javascript
Any clues how to get them to work on the same cookie?

I think you'll have to delete the cookie on the client side first.
 
Thanks Simon... will give it a try...
wardeaux

Simon Wallis said:
It is possible but tricky, I had it working some time ago in an old ASP
application. I can't remember all the necessary tricks, but try doing things
like making the cookie name all uppercase or all lowercase letters and is
only alphanumeric (ie., contains no characters that would get url-encoded).
Also try setting all properties such as domain, path, etc.
 
Back
Top