Solved

Get cookie value on page load

Posted on 2009-04-08
30
709 Views
Last Modified: 2012-08-13
I have a javascript function to get screen resolution:

<SCRIPT LANGUAGE="JavaScript">
            <!-- Begin function to get screen resolution
            var xy = navigator.appVersion;
            xz = xy.substring(0,4);
            document.cookie = "ScreenWidth=" + screen.width
            document.cookie = "ScreenHeight=" + screen.height
            // End -->
</SCRIPT>

I put this function in body section of my INDEX.asp page.
Now i want to use the cookie value on same INDEX.asp page, so i write this ASP code on index page

<form action="<%=sNonSecurePath%>includes/add_item.asp" METHOD="POST" name="frmAdd" onsubmit="return false">
<!-- **** Screen Resolution Start -->
<%'Declare variables
      Dim strScreenWidth
      Dim strScreenHeight
'Read the cookies with the width and height
      strScreenWidth = Request.Cookies("ScreenWidth")
      strScreenHeight = Request.Cookies("ScreenHeight")
'If not cookie was made then say the screen size is 1024x768
      If strScreenWidth = "" Then strScreenWidth = 1024
      If strScreenHeight = "" Then strScreenHeight = 768

      Response.Write("Your screen width is: " & strScreenWidth & "<br>")
      Response.Write("Your screen height is: " & strScreenHeight)
%>
</form>
Now my problem is when the INDEX.asp page load for first time the cookie creates but i dont get the cookie value in variable  strScreenWidth & strScreenHeight for first time.
But when i refresh the page the i get the value in Response.Write.

Pleas solve the problem. Or there is any other way to get the javascript variable value in ASP variable ?
0
Comment
Question by:havj123
  • 13
  • 7
  • 7
  • +1
30 Comments
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24104481
after this code
<%'Declare variables
      Dim strScreenWidth
      Dim strScreenHeight
add two more lines
strScreenWidth = 0
strScreenHeight = 0
0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24104497
or try adding these lines
strScreenWidth = ""
strScreenHeight = ""
0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24104544
try using below as well

strScreenWidth = CInt(Request.Cookies("ScreenWidth"))
strScreenHeight = CInt(Request.Cookies("ScreenHeight"))
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 6

Author Comment

by:havj123
ID: 24104552
Nothing happen, actually my problem is have the values in cookie but when the form loads for the first time i dont get value from Request.cookies.
0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24104554
So finally use the following code


<%'Declare variables
      Dim strScreenWidth
      Dim strScreenHeight
      strScreenWidth = 0
      strScreenHeight = 0
'Read the cookies with the width and height
      strScreenWidth = CInt(Request.Cookies("ScreenWidth"))
      strScreenHeight = CInt(Request.Cookies("ScreenHeight"))
'If not cookie was made then say the screen size is 1024x768
      If strScreenWidth = 0 Then strScreenWidth = 1024
      If strScreenHeight = 0 Then strScreenHeight = 768
 
      Response.Write("Your screen width is: " & strScreenWidth & "<br>")
      Response.Write("Your screen height is: " & strScreenHeight)
%>
</form>

Open in new window

0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24104596
Confirm using alert() function that screen.width and screen.heigh is being executed before the form loads? If not you may consider similar function below where you can assign form values from a java script

function getformvalue(formName, inputName){
var inputObj = document.forms[formName].elements[inputName];
if(inputObj.type){
if(inputObj.type == "text"){
alert(inputObj.value);
}else if(inputObj.type == "checkbox"){
alert((inputObj.checked == true ? inputObj.value : "Unchecked"));
}else if((inputObj.type).substr(0, 6) == "select"){
var selectvalue = '';
var v = 1;
for(var i = 0; i < inputObj.length; i++) {
if(inputObj[i].selected == true){
selectvalue += "value" + v + ": " + inputObj[i].value + "\n";
v++;
}

Open in new window

0
 
LVL 6

Author Comment

by:havj123
ID: 24104611
For first time i got this

Your screen width is: 0
Your screen height is: 0

When i refresh page i got:

Your screen width is: 1280
Your screen height is: 1024  
0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24104644
Ok, then encapsulate your code below in a java function and call this function on the <body onload even of html (body tag is before the form)... then try using the your modified code.

<!-- Begin function to get screen resolution
            var xy = navigator.appVersion;
            xz = xy.substring(0,4);
            document.cookie = "ScreenWidth=" + screen.width
            document.cookie = "ScreenHeight=" + screen.height
            // End -->
0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24104649
Also try using
strScreenWidth = CInt(Document.Cookies("ScreenWidth"))
strScreenHeight = CInt(Document.Cookies("ScreenHeight"))

because there is no any request made yet so refer to the document and check.
0
 
LVL 6

Author Comment

by:havj123
ID: 24104689
Document.Cookies("ScreenWidth") not working in ASP code
0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24104705
use this example that uses java script tag in ASP form body where Java script set's value of form's variable

Enjoy!
<FORM NAME="joe">
<INPUT TYPE="hidden" NAME="burns">
</FORM>
 
<SCRIPT LANGUAGE="javascript">
 
var locate = window.location
document.joe.burns.value = locate
 
var text = document.joe.burns.value
 
function delineate(str)
{
theleft = str.indexOf("=") + 1;
theright = str.lastIndexOf("&");
return(str.substring(theleft, theright));
}
document.write("First Name is " +delineate(text));
 
</SCRIPT> 

Open in new window

0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24104721
Another working example:

<FORM NAME="bull">
Last Name: <INPUT TYPE="text" NAME="bear">
</FORM>

<SCRIPT LANGUAGE="javascript">
var zork = delineate2(text)
document.bull.bear.value = zork
</SCRIPT>

0
 
LVL 6

Author Comment

by:havj123
ID: 24104730
I can insert javascript value in form hidden field. But again my problem is how do i get the Request.Form("hiddenfield") value because my form method is post. And my form is not posted for first time.
0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24104816
whereever you want to get back Form("hiddenfield") value again refer to using java script tage between form using
var text = document.form(0).hiddenfield.value
0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24104844
To get Java script value to ASP is as below

Here is what I do:

Response.write "<script LANGUAGE=" & """" & "JavaScript" & """" & ">" & chr(10)
Response.write "function Messaging(val) {" & chr(10)
Response.Write " winname=" & """" & "MessagingWindow" & """" & chr(10)
Response.Write " msgWin=window.open(" & """" & MainURL & "Messaging/Messaging.asp?SiteID="
& SiteID & "&ID=" & """" & "+val,winname," & """" & "width=480,height=480,toolbar=0,scrollbars=0,status=0"
& """" & ")" & chr(10)
Response.Write " if (navigator.appName == " & """" & "Netscape" & """" &
" && parseInt(navigator.appVersion) >= 3)" & chr(10)
Response.Write " { msgWin.focus(); }" & chr(10)
Response.Write " if (navigator.appName == " & """" & "Microsoft Internet
Explorer" & """" & " && parseInt(navigator.appVersion) >= 3)" & chr(10)
Response.Write " { msgWin.focus(); }" & chr(10)
Response.Write " }" & chr(10)
Response.Write "</script>"%>

0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24104849
sorry above example is how to get ASP VALUE to Java script.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 250 total points
ID: 24105207
You are mixing clientside javascript and server side asp

When the page loads for the first time all it sees is the complete ASP and javascript as sent to the browser.

The asp will NOT see what the javascript sees unless you send an http request to the server with the data

the can be done using ajax, but since you set a cookie, you can also just load an image from the server to send the cookie back to the server
0
 
LVL 5

Expert Comment

by:mfhorizon
ID: 24106469
yes, and in some cases provide the page is not full of controls, developers submit webpage to itself so that page could load once again and all variables initialized.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24106537
So is that what you did in your script, which I would write like this

. . .%><script type="text/javascript">
function Messaging(val) {
  var winname="MessagingWindow";
  var msgWin=window.open('<%= MainURL %>Messaging/Messaging.asp?SiteID=<%= SiteID %>&ID='+val,winname,'width=480,height=480,toolbar=0,scrollbars=0,status=0');
  if (msgWin && msgWin.focus) msgWin.focus(); 
}
</script><% . . .

Open in new window

0
 

Assisted Solution

by:Figou91
Figou91 earned 250 total points
ID: 24106625
Your javascript is executed after ASP has been processed, so first time your cookie is empty.
You should make a first page with your javascript code + redirection to second one with ASP code
0
 

Expert Comment

by:Figou91
ID: 24106655
For example :

Javascript.html :
<html><head>
<meta http-equiv="refresh" content="1; url=page2.asp" />

<SCRIPT LANGUAGE="JavaScript">
            <!-- Begin function to get screen resolution
            var xy = navigator.appVersion;
            xz = xy.substring(0,4);
            document.cookie = "ScreenWidth=" + screen.width
            document.cookie = "ScreenHeight=" + screen.height
            // End -->
</SCRIPT>
</head>
<body>
</body>
</html>


PAGE2.asp
your asp code to read the cookie
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24106656
erm yes, we have established that
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24106686
That is a horrific code.

Why on earth use http-equiv to redirect - it is useless if the javascript was not executed and no need for a cookie if you call the server anyway

Instead do

<html>
<head>
<script type="text/javascript">
location.replace("redirect.asp?ScreenWidth="+screen.width+"&ScreenHeight=" + screen.height);
</script>
</head>
<body>
</body>
</html>

Open in new window

0
 
LVL 6

Author Comment

by:havj123
ID: 24113446
Thanks you all for your comments. Ur comments are useful, but i havent got solution. I think it is impossible to get cookie value on same page. So i dont want further comments on that question.

Thanks
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24113814
So do you have a solution, please share it. If not then the answer is: You cannot do that and it WAS given here
0
 
LVL 6

Author Comment

by:havj123
ID: 24128609
The solution is i cant create and get the cookie value on the same page when the page is load at first time. Because the form method is POST and i can get the server request for first time page load.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24128780
So we did answer your question
0
 
LVL 6

Author Comment

by:havj123
ID: 24144927
No.......
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24145314
#24105207 and repeated by Figou91 #24106625
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
The viewer will learn how to dynamically set the form action using jQuery.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

766 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