• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Trying to combine ASP and javascript to pick up clientside screen resolution

Hi,

I am bouncing my pages off of the following code to try to pickup the screen resolution of the user in javascript and then bounce back to the referring page:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>

<% RefPage = request.ServerVariables("HTTP_REFERER") %>

<script language="javascript">
res = "&res="+screen.width+"x"+screen.height+"&d="+screen.colorDepth
top.location.href=RefPage&"?"&res
</script>

However, the javascript is not recognizing the RefPage variable. What am I doing wrong?
(disclaimer: I know almost nothing about javascript).
0
polaatx
Asked:
polaatx
  • 7
  • 5
1 Solution
 
ColosseoCommented:
Hi polaatx

for the javascript to recognise the refPage variable you have to enclose it in the <%=RefPage%> tags to in your code

<script language="javascript">
res = "&res="+screen.width+"x"+screen.height+"&d="+screen.colorDepth
top.location.href=<%=RefPage%>&"?"&res
</script>

Give that a go

Scott
0
 
ColosseoCommented:
actually try this instead

top.location.href="<%=RefPage%>?"+res;

havent got a webserver here so i cant test it sorry :)

Cheers

Scott
0
 
polaatxAuthor Commented:
Hi Scott,

I'm still getting a refpage undefined error.

Here's what I'm trying to do:

This code will be on top of every page of my site:

<%
if Session("screenres") <> "" then
response.Redirect("detectscreen.asp")
else
Session("screenres") = request.QueryString("res")
end if
%>

My logic is that if session("screenres") is not present yet, then redirect to detectscreen.asp, which contains just the following code:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>

<% RefPage = request.ServerVariables("HTTP_REFERER") %>

<script language="javascript">
res = "&res="+screen.width
top.location.href="<%=RefPage%>?"+res;
</script>


Detectscreen.asp picks up the client's screen width, puts it in a querystring and bounces back to the referring page.

Then the referring page creates Session("screenres") = request.QueryString("res")  and I should have the client's screen size from then on.

What do you think I'm doing wrong?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
ColosseoCommented:
Hi

try changing the function to this

<script language="javascript">
res = "res="+screen.width
top.location.href="<%=RefPage%>?"+res;
</script>

notice the & has been removed from res = "&res

if that doesnt work try adding

alert("<%=RefPage%>?"+res);

before the top.location line and post back here what the alert says

cheers

scott
0
 
polaatxAuthor Commented:
Scott:

I changed the code to this:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<% RefPage = request.ServerVariables("HTTP_REFERER") %>
<script language="javascript">
res = "res="+screen.width
top.location.href="<%=RefPage%>?"+res;
</script>

And for some reason the page starts bouncing back to itself over and over, eventhough I was careful to go to it from another page so it would have referrer to go bounce back to.

For alert, this code ...

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<% RefPage = request.ServerVariables("HTTP_REFERER") %>
<script language="javascript">
res = "res="+screen.width
alert("<%=RefPage%>?"+res);
</script>

produces this: ?res=1024

So the querystring is good. But can't figure out why it doesn't record the referrer correctly.
0
 
ColosseoCommented:
hmm how about hardcoding just to test.

Set the RefPage = to the name of the page it is coming from.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<% RefPage = "nameofsubmittingpage.asp" %>
<script language="javascript">
res = "res="+screen.width
alert("<%=RefPage%>?"+res);
</script>

this will tell us if there is a problem with request.ServerVariables("HTTP_REFERER")

Scott
0
 
polaatxAuthor Commented:
It produces:

nameofsubmittingpage.asp?res=1024

0
 
ColosseoCommented:
looks like a problem with
request.ServerVariables("HTTP_REFERER")

i will have a look
0
 
polaatxAuthor Commented:
Scott, It is working now. When I click a URL to detectscreen.asp, it is bouncing back to any page that refers to it with "res" value in querystring. Try it here: http://www.aurafellows.net/aura/mail_sample.htm and you will get back a querystring.

The problem I think was this code on top of the referring page I built to try to detect session variable:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>

<%

if Session("screenres") = "" then
response.Redirect("detectscreen.asp")
else
Session("screenres") = request.QueryString("res")
end if
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<BODY>
<p><a href="detectscreen.asp">Go to detectScreen.asp</a><p>
screen resolution request querystring variable = <% = request.QueryString("res") %><br>
screen resolution session variable = <% = Session("screenres") %>
</BODY></HTML>

Right now, if the session variable is present, detectscreen.asp goes into a perpetual self-bouncing.
If the session variable is NOT present, the body does not show me the value of neither the session or the querystring.


The detectscreen.asp code is this now:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<% RefPage = request.ServerVariables("HTTP_REFERER") %>
<script language="javascript">
res = "res="+screen.width
top.location.href="<%=RefPage%>?"+res;
</script>


0
 
ColosseoCommented:
Well spotted there pol :)

we might now need to think about some code to only add the res to the querystring if its not there already AND to maintain anyother existing querystring parameters

scott
0
 
polaatxAuthor Commented:
How about this?

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%  
RefPage = request.ServerVariables("HTTP_REFERER")
if request.QueryString("res") <> "" then
response.Redirect(RefPage)
else
%>
<script language="javascript">
res = "res="+screen.width
top.location.href="<%=RefPage%>?"+res;
</script>
<% end if %>
0
 
ColosseoCommented:
yeah that should do it although you dont need the else just end if because if it calls the redirect the script wont run.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%  
RefPage = request.ServerVariables("HTTP_REFERER")
if request.QueryString("res") <> "" then
response.Redirect(RefPage)
end if
%>
<script language="javascript">
res = "res="+screen.width
top.location.href="<%=RefPage%>?"+res;
</script>

you could instead put the if request.querystring("res") <> "" then check on every page to prevent it ever going to detectscreen.asp if it doesnt need to rather than going to detectscreen.asp always

also perhaps change the script too

<script language="javascript">
var current_URL = "<%=RefPage%>";
res = "res="+screen.width
if (current_URL.indexOf("?")==-1) {
  top.location.href=current_URL+"?"+res;
} else {
  top.location.href=current_URL+"&"+res;
}
</script>

what this should do is if there is no querysting in the url (ie no ?) it will add ?res= to the querystring wheras if there is already a querystring in the url (ie subpage.asp?val1=2&val2=3) then it should return subpage.asp?val1=2&val2=3&res=

Hope that makes sense

Scott
0
 
davidlars99Commented:
what if add    

if querystring("query")<>999 then
         top.location.href="<%=RefPage%>?"+res+"&query="+<%=999%>;
end if




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.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now