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.
hitman3030Asked:
Who is Participating?
 
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
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
 
hitman3030Author Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
hitman3030Author Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
hitman3030Author Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
hitman3030Author Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
hitman3030Author Commented:
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
 
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.