Link to home
Start Free TrialLog in
Avatar of polaatx
polaatxFlag for United States of America

asked on

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).
Avatar of Colosseo
Colosseo
Flag of United Kingdom of Great Britain and Northern Ireland image

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
actually try this instead

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

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

Cheers

Scott
Avatar of polaatx

ASKER

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?
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
Avatar of polaatx

ASKER

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.
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
Avatar of polaatx

ASKER

It produces:

nameofsubmittingpage.asp?res=1024

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

i will have a look
Avatar of polaatx

ASKER

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>


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
Avatar of polaatx

ASKER

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 %>
ASKER CERTIFIED SOLUTION
Avatar of Colosseo
Colosseo
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what if add    

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