Solved

What is the earliest event a cookie written in HEAD tag by javascript can be accessed in .net page life cycle?

Posted on 2013-01-06
11
323 Views
Last Modified: 2013-01-07
I have javascript writing a cookie just after the HEAD tag. I can retrieve the cookie with vb.net on the postpack of the page, but not on the Page Load event of that initial rendering. Is there a way I can write the cookie if js and access the value in .net in that same initial rendering?

I think I need a trigger event fired after page load (or a delay?). I have also tried writing the value to a text field and reading it from there, but the value is not there  yet when the code checks, but it is there when the page is finally loaded, so it's pretty clear I'm asking too early in the cycle.

IMPORTANT. This value is used to load custom images on the screen, so it needs to be as early as possible.
0
Comment
Question by:hitman3030
  • 6
  • 5
11 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 38748940
No.  Cookies are sent from the browser in the HTTP (page) Request header and returned/set in the HTTP Response header.  After that, when the page is in the browser, you can only read and set them with javascript.  To get info back to your .NET code, you would have to use AJAX.

Other than using AJAX to load your images, you need a different plan.
0
 

Author Comment

by:hitman3030
ID: 38749358
Ok, I understand. Plan B.

Here is what I am doing. I need some guidance in the best way to incorporate ajax to accomplish it.

I have developed a responsive site that works well with any device, including mobile-- but I want to deliver smaller images to speed up the loading for slow/smaller screens. Thus far:

1) I clock the download of a small (20k) image to get a rough estimate of client's connection.

2) Using jquery, I also get the screen width.

3) I then categorize the enviorment as width+speed (i.e. smallslow, smallfast, bigslow, bigfast)

4) I store this single variable in a cookie which is available on all subsequent page calls.

5) Image tags are loaded initially without a specified src in the aspx file.
In .net code on subsequent pages, I can retrieve the cookie and set images src using this variable in a select case.

THE PROBLEM:
The very first time a client request a page from the site, this cookie is not available to the .net Page Load event. I need to pass it back to the aspx page while the page is loading that first time.

I have not been able to find an example of how to do this with ajax. Is there a way to do this?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38749535
Any HTTP Request will pass the cookie back including an AJAX request.  Just have your jQuery make the AJAX request after it sets the cookie.
0
 

Author Comment

by:hitman3030
ID: 38749894
I'm sorry, I have not been coding for a few years, and jQuery is new to me.
I have put together this simple code. But my request.form value is nothing in debug.
Debugging the js shows that the value is loaded in the ajax post and I get no errors.

Am I trying to read the post data wrong?

Here is the ajax after setting cookie.

   var sizespeed = getCookie('cSizeSpeed');
   $.ajax({ url: "http//www.upmyscore.com/lp", data: { "sizespeed": sizespeed }, type: "POST" });

Open in new window


Here is the read code in the .net PageLoad event:

 
strSpeedSize = System.Web.HttpContext.Current.Request.Form("sizespeed")

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38749912
If you have already set the cookie, just read the cookie on the .NET page that the AJAX routine calls.  Cookies for a site are sent with Every page request.  The actual AJAX data for this doesn't even matter.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:hitman3030
ID: 38749989
I have created this function to be called by the ajax:

    <System.Web.Services.WebMethod> _
    Public Shared Function GetSpeed()
        Dim strSpeedSize As String = ""
        If Not IsNothing(System.Web.HttpContext.Current.Request.Cookies("cSizeSpeed")) Then
            strSpeedSize = System.Web.HttpContext.Current.Request.Cookies("cSizeSpeed").Value
        End If 

Open in new window


However, in debug it does not hit my break.  My js call does break when debugging in IE.
The full call is:

   var sizespeed = getCookie('cSizeSpeed');
   $.ajax({ url: "http//www.upmyscore.com/lp/lp.aspx/GetSpeed", data: { "sizespeed": sizespeed }, type: "POST" });

Open in new window


Is there anything obvious that I am missing?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38750001
Is this the same cookie you were talking about in the beginning?  If so, use the same code to read the cookie.  After all, that is the goal.
0
 

Author Comment

by:hitman3030
ID: 38750030
Yes, the code below is the same way the cookie is read on any page view except the very first. And it does work.
strSpeedSize = System.Web.HttpContext.Current.Request.Cookies("cSizeSpeed").Value

Open in new window


In my immediate previous post, you can see the js is pulling the same cookie just before the ajax call--and it does show value in debug.

It appears it is the AJAX calling the GetSpeed function is the problem, because it does not hit my break point set here:
2.     Public Shared Function GetSpeed()  

This is the call to GetSpeed()
$.ajax({ url: "http//www.upmyscore.com/lp/lp.aspx/GetSpeed", data: { "sizespeed": sizespeed }, type: "POST" });

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38750084
I assume "lp.aspx" is the name of the page file.  Why do you have "/GetSpeed" after it?  I'm not a .NET programmer but I do know how cookies work and it looks like you're trying too hard.  Every HTTP page Request including AJAX calls, CSS links, external JS files, Everything includes the cookies in the Request header so the server can read them.  The AJAX call doesn't actually need any data to pass the cookie to the server code.
0
 

Author Closing Comment

by:hitman3030
ID: 38752582
i went ahead and marked the first response as solution since it the answer to the question I asked. I have not got the Plan B solution working yet, but I think it should be posted as a separate issue. Thanks David.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38752603
You're welcome, glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

862 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

25 Experts available now in Live!

Get 1:1 Help Now