Solved

ASP.net modify cookie created in javascript

Posted on 2008-11-01
6
505 Views
Last Modified: 2008-11-04
Hi

I am using a cookie in javascript like this.....answer to previous question.
            var intX = 123;
            document.cookie = "xPos=!~" + intX + "~!";
on a page say Page1.aspx so that postbacks on same page remember value

However when clicking a button on e.g. Page2.aspx which links to Page1.aspx I want to delete the cookie before going to Page1.aspx (so it isn't there anymore and a check to
if(strCook.indexOf("!~")!=0)
is false,
instead of true as it is after doing document.cookie = "xPos=!~" + intX + "~!";

Never used cookies before so this is all new.

Thanks in advance for help.
0
Comment
Question by:rwallacej
  • 3
  • 2
6 Comments
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 250 total points
ID: 22856541
ASP doesn't particularly care how or where a cookie got created  it's just a cookie like any other.

So the real question is just, "How does one delete a cookie in ASP.net?"

Google pointed me to this page:
http://www.codeproject.com/KB/aspnet/aspnetcookies.aspx

There we see this line of code (with your cookie's name), which tells the browser the cookie should have expired 30 years ago, effectively deleting it:

Response.Cookies["xPos"].Expires = DateTime.Now.AddYears(-30);
0
 

Author Comment

by:rwallacej
ID: 22856631
Hi,

Thanks for link/code.
I tried this in advance of posting question.
Response.Cookies("xPos").Expires = DateTime.Now.AddYears(-30)

Now I tried
Dim c As System.Web.HttpCookie = Response.Cookies("xPos")
to check existing but the value of it is blank and data #12:00:00 AM#

Despite setting
Response.Cookies("xPos").Expires = DateTime.Now.AddYears(-30)

the Page1.aspx still thinks it is there, and has value it had as if it wasn't deleted

So something isn't working right still - cookie still exists on page loading up
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22856954
if i understand your problem correctly you want to delete the cookie when the page loads because as the page is loading for the first time the xposition is not there

if i am correct then in you page load you can add the following code

if (IsPostBack == false)
     Response.Cookies.Remove("Cookie Name");

and you are setting the cookie from your javascript code
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:rwallacej
ID: 22857163
ragi0017,

you are correct.
the first time the page is loaded, I do not want xPos to be there; I do not want to remember xPos from previous visits to this page, only during postbacks

the cookie is set in javascript when user uses a scrollbar to remember the X position of scrollbar after postback.

I have found that even with
if (IsPostBack == false)
     Response.Cookies.Remove("Cookie Name");

set, xPos is still remembered.

I also tried
Response.Cookies("xPos").Value = 0
to set the value to zero but it still retains the value set in javascript / the value isn't deleted (so the horizontal navigator always scrolls to xPos)

not sure what's happening
0
 
LVL 26

Assisted Solution

by:Anurag Thakur
Anurag Thakur earned 250 total points
ID: 22857414
can you have a look at the following link and see the function in javascript where there are cases for saving, loading and deleting cookies from javascript
http://www.codeproject.com/KB/scripting/Javascript_PageCookie.aspx

what my intention is that on the page load you call a javascript function and then delete the cookie
but you will have to make sure that its deleted on the page load and not on postback
0
 

Author Comment

by:rwallacej
ID: 22877763
cheers for help
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question