Get cookie value on page load

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 ?
LVL 6
havj123Asked:
Who is Participating?
 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
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
 
mfhorizonCommented:
after this code
<%'Declare variables
      Dim strScreenWidth
      Dim strScreenHeight
add two more lines
strScreenWidth = 0
strScreenHeight = 0
0
 
mfhorizonCommented:
or try adding these lines
strScreenWidth = ""
strScreenHeight = ""
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mfhorizonCommented:
try using below as well

strScreenWidth = CInt(Request.Cookies("ScreenWidth"))
strScreenHeight = CInt(Request.Cookies("ScreenHeight"))
0
 
havj123Author Commented:
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
 
mfhorizonCommented:
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
 
mfhorizonCommented:
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
 
havj123Author Commented:
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
 
mfhorizonCommented:
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
 
mfhorizonCommented:
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
 
havj123Author Commented:
Document.Cookies("ScreenWidth") not working in ASP code
0
 
mfhorizonCommented:
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
 
mfhorizonCommented:
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
 
havj123Author Commented:
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
 
mfhorizonCommented:
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
 
mfhorizonCommented:
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
 
mfhorizonCommented:
sorry above example is how to get ASP VALUE to Java script.
0
 
mfhorizonCommented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
Figou91Connect With a Mentor Commented:
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
 
Figou91Commented:
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
 
Michel PlungjanIT ExpertCommented:
erm yes, we have established that
0
 
Michel PlungjanIT ExpertCommented:
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
 
havj123Author Commented:
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
 
Michel PlungjanIT ExpertCommented:
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
 
havj123Author Commented:
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
 
Michel PlungjanIT ExpertCommented:
So we did answer your question
0
 
havj123Author Commented:
No.......
0
 
Michel PlungjanIT ExpertCommented:
#24105207 and repeated by Figou91 #24106625
0
All Courses

From novice to tech pro — start learning today.