A JAVA-based method for detecting the user's screen resolution

Introduction

Oftentimes, the question is asked how one may detect a web user's screen resolution and redirect them to a page designed specifically for that resolution. Below is a JAVA-based solution of doing so as well as a discussion on why you shouldn't do so.

The Problem

It is the goal of every web author to create a site that is useful and informative, and at the same time, visually appealing. Often, with the web, this is not so easily done. This is because you don't know how your visitor's computer will be setup. Is the screen resolution higher then your own? Will the website have too much white space as a result? Or perhaps it will appear cramped because the visitor's screen resolution is set smaller than your own.

The JAVA Solution

JAVA can provide you with a solution to your problem. The screen resolution can be found via the java.awt.Toolkit class and the "redirection" can be accomplished via the java.applet.AppletContext.showDocument() function. The source code follows or you can download the precompiled applet (zipped along with HTML and TXT documentation).

import java.applet.*;
import java.awt.*;
import java.net.*;

public class ScreenRes extends java.applet.Applet {
  public void start() {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    String file = null;
    URL url = null;

    switch(screenSize.width) {
      case 640: file = "640.html"; // 640x480
            break;

      case 800: file = "800.html"; // 800x600
            break;

      case 1024: file = "1024.html"; // 1024x768
            break;

      case 1280: file = "1280.html"; // 1280x1024
            break;

      case 1600: file = "1600.html"; // 1600x1200
            break;

      default:  file = "other.html";
            break;
      }

    try { url = new URL(getDocumentBase(), file); }
    catch ( MalformedURLException e) {
      url = null;
      }

    if (url != null) {
      getAppletContext().showDocument(url);
      }
    }
  }

...and insert the following tag into the BODY of your HTML document:

<APPLET CODE="ScreenRes.class" WIDTH=1 HEIGHT=1></APPLET>

The above applet will cause a JAVA-enabled browser, with JAVA turned on, to redirect the user to 640.html if the screen resolution is 640x480, to 800.html if the screen resolution is 800x600, etc. Go ahead... give it a try.

Now That You Know of a Solution... Don't Use It!

The ability to detect a user's screen resolution and redirect her accordingly would seem a very compelling thing to do. However, it has several very serious reasons as to why you should not:

  1. Doing this would force you to maintain several versions of your webpage, or presumably, your entire website. You would need to maintain a version for each screen resolution that you wish to support effectively doubling, tripling, etc. your work.
  2. Not all browsers support JAVA and those that do allow the user to disable JAVA. This would make it so that they would not be redirected and your work will have been wasted.
  3. Your visitors will notice the redirection. They will see the screen load the initial page and then flash and load the new page. This tends to be a very annoying thing.
  4. Most importantly, the applet detects the screen's resolution and not the browser's width which is more important to the designer. There is no way to detect how large the browser window is. Knowing what the screen resolution is will not help you in better designing a website.

Your best bet is to markup your document intelligently. Do not design for a specific resolution or even a specific number of colors. Rather, design your page for everyone using intelligent, degradable markup.


[Section Index] [Home Page]