Solved

Redirecting web page based on screen resolution

Posted on 2011-09-16
5
298 Views
Last Modified: 2012-08-13
I am looking into serving different versions of my website based on the visitor's screen resolution. The objective is to render font size and images in a pleasing and legible manner.  What if the visitor has blocked reporting of his system's screen resolution?  Will the javascript on my site know how to redirect that visitor?
0
Comment
Question by:ddantes
[X]
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
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:nixkuroi
ID: 36552840
Onload, you can set the height of your content div to various sizes and determine if there is a scroll bar.  

try something like this:
function checkHeight()
{
  var contentDiv = document.getElementById("contentDiv");
  var hwArray = new Array();
  hwArray[0]=new Array(640,480);
  hwArray[1]=new Array(800,600);

  var pageSizeIndex=-1;
  for (i=0;i<hwArray.length;i++)
  {
    contentDiv.style.width=hwArray[i][0];
    contentDiv.style.height=hwArray[i][1];
    var docHeight = $(document).height(); // needs JQuery
    var scroll    = $(window).height() + $(window).scrollTop(); 
    if (docHeight == scroll) // There ARE Scrollbars
    {
        pageSizeIndex=i;
        break;
    }
  }
// the PREVIOUS size is the one you want.
   if (pageSizeIndex>0)
      pageSizeIndex--;
// now set your page size to the hwArray
   if (pageSizeIndex>=0)
   {
     contentDiv.style.width=hwArray[pageSizeIndex][0];
     contentDiv.style.height=hwArray[pageSizeIndex][1];
   }
}

Open in new window

0
 

Author Comment

by:ddantes
ID: 36552975
Thank you for your comment.  I think I may not have articulated my question clearly.  I am thinking about serving different versions of my website, based on the viewer's screen width (I should have specified that before).  Here is the type of code I am using:

   if (screen.width >=1600 && screen.width <= 2000) // width 1600-2000
        window.location ="SiteA/index.shtml";      
    else if (screen.width >=1200 && screen.width <=1599) // width 1200-1599
        window.location = "SiteB/index.shtml";
    else if (screen.width >=700 && screen.width <=1099) // width 700-1099
        window.location = "SiteC/index.shtml";

Now, my question relates to visitors who have blocked reporting of their screen resolution.  When such a visitor arrives, will the above javascript be able to triage him/her to the appropriate website version?
0
 
LVL 3

Expert Comment

by:nixkuroi
ID: 36553043
Ah sorry, let me explain.  I was thinking that if someone was somehow blocking their screen width from being read, you could basically test the page to see at what size a scroll bar would be created instead.  This assumes that their browser is maximized (as in mobile browsers) and effectively tells you the browser size implicitly without asking for the screen height or width.  If you can detect when their window scrolls, you'll have a hint as to what size their monitor is and can redirect to another page.
0
 

Author Comment

by:ddantes
ID: 36553159
Thank you for clarifying.  In order to simplify this (closer to my level of competence!)  can I just add:
else //if all else window.location.replace("siteC/index.shtml")
to the code which I posted previously?  I am happy with the performance of that code, but 20% of visitors to my website don't reveal their screen resolution, and I don't want a glitch which fails to serve them a web page.
0
 
LVL 3

Accepted Solution

by:
nixkuroi earned 125 total points
ID: 36554208
Yep.  you could just remove the last condition and leave it at:


   if (screen.width >=1600 && screen.width <= 2000) // width 1600-2000
        window.location ="SiteA/index.shtml";      
    else if (screen.width >=1200 && screen.width <=1599) // width 1200-1599
        window.location = "SiteB/index.shtml";
    else  // width whatever else
        window.location = "SiteC/index.shtml";
   
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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)

734 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