Solved

Get cookie value on page load

Posted on 2009-04-08
30
707 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
Comment Utility
after this code
<%'Declare variables
      Dim strScreenWidth
      Dim strScreenHeight
add two more lines
strScreenWidth = 0
strScreenHeight = 0
0
 
LVL 5

Expert Comment

by:mfhorizon
Comment Utility
or try adding these lines
strScreenWidth = ""
strScreenHeight = ""
0
 
LVL 5

Expert Comment

by:mfhorizon
Comment Utility
try using below as well

strScreenWidth = CInt(Request.Cookies("ScreenWidth"))
strScreenHeight = CInt(Request.Cookies("ScreenHeight"))
0
 
LVL 6

Author Comment

by:havj123
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Document.Cookies("ScreenWidth") not working in ASP code
0
 
LVL 5

Expert Comment

by:mfhorizon
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 5

Expert Comment

by:mfhorizon
Comment Utility
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
Comment Utility
sorry above example is how to get ASP VALUE to Java script.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
erm yes, we have established that
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
So we did answer your question
0
 
LVL 6

Author Comment

by:havj123
Comment Utility
No.......
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
#24105207 and repeated by Figou91 #24106625
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
This article will show, step by step, how to integrate R code into a R Sweave document
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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

9 Experts available now in Live!

Get 1:1 Help Now