Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I access cookie created in javascript?

Status
Not open for further replies.

dpdoug

Programmer
Nov 27, 2002
455
0
0
US
There are so many instances that I need create a cookie in javascript and access the same cookies from asp.net. Any time you create a control that will need to set a cookie from the server you not only have to do a postback, but webserver controls add more over head to your page.

For example, I have a lot of links that I have to store state information every time one is clicked. So I use the LinkButton. I discovered however, the LinkButton adds much more overhead to your page than just ordinary HTML anchor tags. This adds to the time it takes the server to render the page.

But if I could set cookies in javascript then read the cookie on the server in asp.net, I save on postbacks, page overhead and can take advantage of the use of HTML controls, which are much lighter, instead of server controls.
 
i've done this on one of my sites, set the cookie in js, then from your code u can use

Request.Cookies["name"].Value

Pete
 
Is this true? If so, this will revolutionize my programming issues. I have always thought it couldn't be done, so unfortunately -- I never even tried it. [sad]

I sure will now though!

Thanks a lot! [thumbsup2]
 
dpdoug postback and let us know if it worked and a sample of the code please.

Thanks,
MJ
 
Yes I tested it out and it definately does work. Thank God.

The only problem with getting the values from the scripting side you have to parse the cookie.

Set the cookie in vbscript:
Code:
Function SetCookie()
	dim my, Exp, dt
	dt = date() + 30	 	
	my = document.all("lstMonthYear").options(document.all("lstMonthYear").SelectedIndex).text
	Exp = "expires=" & dt
	document.cookie = "MonthYear=" & my & ";" & Exp
        GetCookieValue("MonthYear") '(see function below) 
End Function

Display the cookie value in vb.net:
Code:
Me.lblMonthYear.Text = Request.Cookies("MonthYear").Value

Parse the value in client-side vbscript:
Code:
Function GetCookieValue(CkName)

	CkNameLen = Len(CkName)

	If InStr(Document.Cookie, CkName) = 0 Then
		Alert "Cookie value not found"
	Else
		CkValStart = InStr(Document.Cookie, CkName) + CkNameLen + 1
		If InStr(CkValStart, Document.Cookie, ";") = 0 Then
			CkVal = Mid(Document.Cookie, CkValStart)
		Else
			CkValEnd = InStr(CkValStart, Document.Cookie, ";")
			CkValLen = CkValEnd - CkValStart
			CkVal = Mid(Document.Cookie, CkValStart, CkValLen)
		End If
	End If
	document.all("lblMonthYear").innerText = CkVal
End Function

This also works in Javascript.

There's one caveat though, I found. If you create the cookie in .NET with a certain expiration date, you have to use the same expiration date in vbscript, or it won't be added to the cookie.

This revelation is tremedously great for me as it cuts way back on the postbacks needed to store information in cookies and also cuts down the number of web server controls that create lots of overhead, thus speeding up the app.

When you're doing a web app that is very data intensive, it becomes very important to make webforms as light as possible.
 
DpDoug thanks for the reply I'm just starting to develop a couple of different web sites and am looking to learn as much as possible.

Thanks Again for the info.

MJ
 
To bring this thread back....

I was wondering how to do the opposite: Use JavaScript to access a cookie created in ASP.NET. Thanks for your time.

Nick Ruiz
CO-OP Intern, PPL Electric Utilities
Webmaster, DealMerchant.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top