Redirecting web page based on screen resolution

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?
ddantesAsked:
Who is Participating?
 
nixkuroiConnect With a Mentor Commented:
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
 
nixkuroiCommented:
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
 
ddantesAuthor Commented:
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
 
nixkuroiCommented:
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
 
ddantesAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.