Filed Under (Design, Javascript Code Samples) by Srikanta on June-11-2007

Handling Screen Resolution Issues
Browser differences are only half the problem you’ll encounter when attempting to create a universally viewable Webpage. The conditions in which the browser is running, operating system and screen resolution to name a couple, also create problems. Luckily, there is a built-in JavaScript object, Screen, which will help you with this. The Screen object contains information on the display screen’s size and color depth.

Screen Properties
There are several properties that are built into the Screen object in order for you to more effectively use it. Here is a complete list of their names and descriptions of each:

availHeight : This property returns the height of the screen in pixels. The height of any components of the operating system’s interface—such as those of the Windows Taskbar—are subtracted automatically.

availWidth : This property returns the width of the screen in pixels. The width of any components of the operating system’s interface—such as those of the Windows Taskbar—are subtracted automatically.

colorDepth : This property represents the color depth of a color table, if one is in use. If no color table is being used, this property is the Screen.pixelDepth property.

height : This property returns the height of the screen in pixels.

pixelDepth : This property returns the color resolution of the display screen in bits per pixel.

width : This property returns the width of the screen in pixels.

Even though the Screen object is available to return information about the client’s screen, it is not a good idea to give the user a message informing him that he’s not viewing your Web page under ideal conditions. It’s your responsibility as the programmer to make your Web pages work with as many screen variations as possible.

Working with Different Screen Settings

How to get all of your content on the page and how your images will look when they are displayed are two of the most important issues you’ll encounter when dealing with different screen settings.

Getting all of your content onto a page is relatively easy because HTML has built-in features that can be used to create content that fits on any resolution. The best way to make sure that all of a page’s content fits in any screen resolution is to enclose the entire content in a table (which may or not have a border) and set its width property to 600 pixels.

Code (javascript)

<html>

  <head>
    <title>
      Containing Content
    </title>
  </head>

  <body>
<table width="600">
<tr>
<td>
      <center>
        <font size=6>JavaScript</font>
      </center>

      <code>
        If you place all your pages content into a table that has a
        defined width of 600 pixels, it will all fit into a browser
        that is being used under any screen resolution <b>and</b> it
        will look exactly the same under any of the many screen
        resolutions.
      </code>
</td>
</tr>
</table>

  </body>

</html>
 

Because the smallest standard screen resolution is 640×480 pixels, your Web page will fit into any browser running under any of the many screen resolutions. Not only will all your content fit on the page, but it will also look exactly the same under any condition. The only adverse side effect of using this technique occurs when the user is viewing your Web page in a browser window that is not maximized. If this is the case, some of the content of your page will be hidden, and the user will have to use the scroll bar to view it. Fortunately, with your knowledge of the window and screen objects, this side effect can easily be avoided.

Here’s how:

Code (javascript)

<html>

  <head>
    <title>
      Resizing Small Windows
    </title>

    <script language="JavaScript">
    <!--
      function onLoad()
      {
        self.moveTo( 0, 0 );
        self.resizeTo( screen.availWidth, screen.availHeight );
      }
    // -->
    </script>

  </head>

  <body onLoad="JavaScript: onLoad();">
<table width="600">
<tr>
<td>
      <center>
        <font size=6>JavaScript</font>
      </center>

      <code>
        If you place all your pages content into a table that has a
        defined width of 600 pixels, it will all fit into a browser
        that is being used under any screen resolution <b>and</b> it
        will look exactly the same under any of the many screen
        resolutions.
      </code>
</td>
</tr>
</table>

  </body>

</html>
 

This example resizes the window as soon as it loads to make sure it is big enough to hold all of the horizontal content without scroll bars. As most users will become annoyed by having their windows resized unnecessarily, it’s a good idea to put a check in the onLoad() function above, so that the window will only resize if it is necessary in order to show all of the content:

Code (javascript)

function onLoad()
{
  if( self.innerWidth < 600 )
  {
     self.moveTo( 0, 0 );
     self.resizeTo( screen.availWidth, screen.availHeight );
  }
}
 

As a last resort, you can always display a message for the user if he is using a screen resolution that you feel your content or images just cannot accommodate. Here is a modified onLoad() function again that does just that:

Code (javascript)

function onLoad()
{
  if( self.innerWidth < 600 )
  {
    self.moveTo( 0, 0 );
    self.resizeTo( screen.availWidth, screen.availHeight );
  }

  if( self.innerWidth < 600 )
  {
    alert( "This Web page was designed to be viewed using a" +
            " screen resolution of 640x480 or larger." );
  }
}
 

This function attempts to resize the window, but if it’s still not large enough, a message is displayed explaining the problem.

Another important thing to remember when writing a Web page for different screen resolutions is that images will appear larger on smaller resolutions. Fortunately, the width and height properties of the HTML tag allow you to dynamically set the size of the image. Here is a typical image tag that dynamically sizes the image to the screen:
(UPDATED: Thanks to Laurie Erickson)

Code (javascript)

<img src="image.jpg"
      onload="this.width=getWidth( 150 );this.height=getHeight( 200 );">
 

That statement, in combination with the following two functions:

Code (javascript)

function getWidth( width )
{
  return( width * screen.width / 1024 );
}
function getHeight( height )
{
 return( height * screen.height / 768 );
}
 

will dynamically resize the image so that it will look the same on any screen resolution. These two functions—getWidth() and getHeight()—assume that the image was designed for screen resolution 1024 × 768, but you can change that by changing the resolution width and height in the return statements.



Sheniya on August 23rd, 2007 at 12:42 pm #

It was very useful for me.
Thanks………

calypso on February 26th, 2008 at 1:11 pm #

I make pages in MS Word. They look great at 1024*768 but not all ppl use that so i hope the table method will work for me! Please send any help at all! I don’t know html but need to.

calypso on February 26th, 2008 at 1:52 pm #

Thanks again to your website here. I tried making a webpage in word with all content in a table and it works in any res whatsoever. Also i set the table properties to 100% and i can see the whole site in a small window!!! Thanks

MOHD on September 5th, 2008 at 7:30 pm #

It was very useful,,

but caw can i use

haw can i fixed that code

Ajay on October 22nd, 2008 at 8:34 pm #

I have already developed a application having 1280 * 768 resolution. How can I not change it to look better in 1024 * 768. I have not used %age or table in javascript.

Thanks

Post a comment
Name: 
Email: 
URL: 
Comments: