Javascript and cookies

Discussion in 'ASP .Net' started by Wardeaux, Jul 2, 2004.

  1. Wardeaux

    Wardeaux Guest

    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, Jul 2, 2004
    #1
    1. Advertising

  2. "Wardeaux" <> wrote in message
    news:...
    > 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.
    --
    John Saunders
    johnwsaundersiii at hotmail
    John Saunders, Jul 3, 2004
    #2
    1. Advertising

  3. Wardeaux

    Wardeaux Guest

    Thanks Simon... will give it a try...
    wardeaux

    "Simon Wallis" <> wrote in message
    news:D...
    > 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.
    >
    > It is definitely possible.
    >
    > Simon.
    >
    >
    > "Wardeaux" wrote:
    >
    > > 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, Jul 6, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Nitulescu

    Response.Cookies vs Request.Cookies

    Alex Nitulescu, Feb 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,459
    Hans Kesting
    Feb 3, 2005
  2. Andy Fish
    Replies:
    3
    Views:
    6,508
    Fredrik Lindner
    Nov 6, 2003
  3. user
    Replies:
    3
    Views:
    649
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Mar 31, 2007
  4. archana
    Replies:
    1
    Views:
    500
  5. _Who
    Replies:
    7
    Views:
    2,637
Loading...

Share This Page