Solved

ASP.net modify cookie created in javascript

Posted on 2008-11-01
6
506 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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

734 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