Solved

Redirecting web page based on screen resolution

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Google adds suggestions 3 69
how to extract window origin in spring 2 75
Problem to file 3 83
React or Angular? 6 81
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn how to count occurrences of each item in an array.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

828 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