spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / domscripting / 1 current pageTo page 2To page 3To page 4To page 5To page 6To page 7
[next]
Developer News
MicrosoftÂ’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads For The JON 2.0

Scripting for 5th Generation Browsers and Beyond

[Editor's note: all "live examples" within this article open in a new window unless otherwise specified.]

Code Reduction

One of the biggest benefits of coding for IE5+ and Netscape 6+ is the code reduction that is obtained by not having to include conditional branches (e.g. If/else statements). For example, let us suppose we wanted to dynamically change the background color of a layer in Netscape 4, Internet Explorer 4, and Internet Explorer 5+/Netscape 6+. We would have to create conditional statements to cater to the different Document Object Models as demonstrated in the below example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Change The Background Color</title>
<script>
function changelayerColor() {
if (document.all)
  document.all.Layer1.style.backgroundColor = 'red';
else if (document.getElementById)
  document.getElementById('Layer1').style.backgroundColor = 'red';
else if (document.layers)
  document.Layer1.bgColor = 'red';
}
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<a href="#" onMouseover="changelayerColor()">Change 
   Layer Background Color</a>
<div id="Layer1" 
     style="position:absolute; width:200px; height:115px; 
     z-index:1; left: 203px; top: 84px; 
     background-color: #0066FF; layer-background-color: #0066FF; 
     border: 1px none #000000"></div>
</body>
</html>

However, if you wanted to code by W3C standards the above script would translate as such: (live example)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>W3C Change Layer Background Color</title>
<script>
function changeColor() {
   document.getElementById('Layer1').style.backgroundColor = 'red';
 }
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<a href="#" onMouseover="changeColor()">Change Layer Background Color</a>
<div id="Layer1" 
    style="position:absolute; width:200px; height:115px; z-index:1; 
    left: 203px; top: 84px; background-color: #0066FF"></div>
</body>
</html>

As is demonstrated in the above example, there is a considerable reduction in the code. The above script functions in Internet Explorer 5+ and Netscape 6+ as these both support the W3C DOM document.getElementById() method. At last a standard way of scripting that is compatible in both browsers! From my perspective, that is the real benefit of coding in such a manner.

Contents:

home / programming / javascript / domscripting / 1 current pageTo page 2To page 3To page 4To page 5To page 6To page 7
[next]



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Perl Pragma Primer · Implement Drag and Drop in Your Web Apps: Part 2 · How to Create an Ajax Autocomplete Text Field: Part 5
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
SQL Server 2005 Express Edition - Part 22 - Upgrading from Microsoft SQL Server Desktop Engine (MSDE) · Vyatta: Downgrades that Pay Off · NetMotion Brings Cross-Network Support to Wireless VoIP


Created: August 16, 2001
Revised: August 16, 2001


URL: http://webreference.com/programming/javascript/domscripting/1/