Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Redirecting web page based on screen resolution

Posted on 2011-09-16
5
Medium Priority
?
305 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
  • 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 500 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

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.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

971 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