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
319 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 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:hitman3030
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
You're welcome, glad to help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

744 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

16 Experts available now in Live!

Get 1:1 Help Now