Solved

Redirecting web page based on screen resolution

Posted on 2011-09-16
5
294 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 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
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.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now