Solved

ASP.net modify cookie created in javascript

Posted on 2008-11-01
6
502 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:rwallacej
Comment Utility
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
Comment Utility
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
Comment Utility
cheers for help
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now