Link to home
Start Free TrialLog in
Avatar of hitman3030
hitman3030

asked on

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

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.
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hitman3030
hitman3030

ASKER

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?
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.
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

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.
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?
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.
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

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.
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.
You're welcome, glad to help.