Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2004-08-22
13
208 Views
Last Modified: 2012-05-05
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
Comment
Question by:polaatx
  • 7
  • 5
13 Comments
 
LVL 15

Expert Comment

by:Colosseo
ID: 11864877
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
 
LVL 15

Expert Comment

by:Colosseo
ID: 11864911
actually try this instead

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

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

Cheers

Scott
0
 

Author Comment

by:polaatx
ID: 11865088
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 15

Expert Comment

by:Colosseo
ID: 11865111
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
 

Author Comment

by:polaatx
ID: 11865354
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
 
LVL 15

Expert Comment

by:Colosseo
ID: 11865370
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
 

Author Comment

by:polaatx
ID: 11865408
It produces:

nameofsubmittingpage.asp?res=1024

0
 
LVL 15

Expert Comment

by:Colosseo
ID: 11865415
looks like a problem with
request.ServerVariables("HTTP_REFERER")

i will have a look
0
 

Author Comment

by:polaatx
ID: 11865450
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
 
LVL 15

Expert Comment

by:Colosseo
ID: 11865526
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
 

Author Comment

by:polaatx
ID: 11865605
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
 
LVL 15

Accepted Solution

by:
Colosseo earned 500 total points
ID: 11865875
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
 
LVL 13

Expert Comment

by:davidlars99
ID: 11867297
what if add    

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




0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Lazy load not working with masonry js 1 22
JavaScript let vs var 5 34
Access RV042 GUI / Browser Issues 25 47
on window close, not beforeunload 6 15
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 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