?
Solved

ASP.net modify cookie created in javascript

Posted on 2008-11-01
6
Medium Priority
?
509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 1000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

765 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