spacer
Yehuda Shiran September 24, 1999
Detecting the Number of Colors
Tips: September 1999

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

The screen object combines several useful properties. You can take advantage of its colorDepth property to find out how may colors are supported. For example, if the user has 256 colors, the value of screen.colorDepth would be 8 (28 = 256).

This property is supported by Navigator 4.0x, Internet Explorer 4.0x, and above. It normally holds one of the following values: 4, 8, 16, 24. The following cross-browser, backward-compatible piece of code shows how you can display an image based on the number of available colors:

<SCRIPT LANGUAGE="JavaScript">
<!--

var bName = navigator.appName;
var bVer = parseInt(navigator.appVersion);
var NS2 = (bName == "Netscape" && bVer == 2);
if (window.screen) {
  var url = (screen.colorDepth >= 16) ? "16bit.gif" : "8bit.gif";
  document.write("<IMG SRC='", url, "' HEIGHT='100' WIDTH='300'>");
} else if (!NS2) {
  document.write("<IMG SRC='8bit.gif' HEIGHT='100' WIDTH='300'>");
}

// -->
</SCRIPT>
<NOSCRIPT>
<IMG SRC="8bit.gif" HEIGHT="100" WIDTH="300">
</NOSCRIPT>

The script may seem a bit complicated, because it accounts for Navigator 2.0x's lack of support for the useful <NOSCRIPT> tag.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business