spacer

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

home / programming / javascript / netscape6 111156
[previous] [next]

Information Technology Auditor (PA)
Next Step Systems
US-PA-Wayne

Justtechjobs.com Post A Job | Post A Resume
Developer News
Google Chrome Playing Catch-Up on Extensions
Open Solutions Alliance Gets New Leadership
Red Hat Spacewalk Expands Linux Management

DOM Tips and Tricks

Browser Sniffing

One new test you can use for DOM-compliance is the document.getElementById test, as follows:

N   = (document.layers) ? true:false;                 // netscape 4
I   = (document.all) ? true:false;                    // ie4+
DOM = ((document.getElementById)&&(!I))?true:false;   // ns6 etc.

There are a number of variations on this theme, but you get the idea. Future versions of this branching logic might add IE5.5+ to the DOM mix with appropriate code.

Creating Elements

Once you've got the branching logic down you would typically create and apply style to your DHTML element by using static HTML and a style sheet or by building up a positioned DIV string dynamically and by using createContextualFragment to append it to the body element, as follows:

function makeDOM(){   
    var text='<DIV ID="bnr" STYLE="position:absolute;overflow:hidden;top:'+bnrTop+'px;';
    text   +='left:+bnrLft+'px;width:'+bnrWid+'px;height:'+bnrHit+'px">';    
    text+='<\/DIV>';
    msgWid=bnrWid-leftPadding;
    var r = document.body.ownerDocument.createRange();
    r.setStartBefore(document.body);
    var df = r.createContextualFragment(text);
}

A better way is to create a new DIV using the DOM in NS6, as follows:

function makeDOM(){
    var elmParentDiv;
    elmParentDiv = document.createElement('div');
    elmParentDiv.id = 'bnr';
    elmParentDiv.style.position = 'absolute';
    elmParentDiv.style.overflow = 'hidden';
    elmParentDiv.style.top = bnrTop + 'px';
    elmParentDiv.style.left = bnrLft + 'px';
    elmParentDiv.style.width = bnrWid + 'px';
    elmParentDiv.style.height = bnrHit + 'px';
    document.body.appendChild(elmParentDiv);
}

Thankfully the Mozilla crew decided to include IE5's proprietary innerHTML method (not in the W3C's DOM but very useful as a shortcut). Instead of the above you can do something like:

function makeDOM() {
    var text='<DIV ID="bnr" STYLE="position:absolute;overflow:hidden;top:'+bnrTop+'px;';
    text   +='left:+bnrLft+'px;width:';+bnrWid+'px;height:'+bnrHit+'px">';
    text+='<\/DIV>';
    msgWid=bnrWid-leftPadding;
    document.getElementById('IEfad1').innerHTML  = text;
}

Referencing Elements

Once you've created a positioned element you would typically grab it with a getElementById then move or replace it. However, you should always use intermediate variables to hold the result of a getElementById since it may cause a large performance hit, especially for more complex scripts. For example,

Instead of:

if (DOM) {
    document.getElementById('IEfad1').innerHTML  = TopnewsStr;
    document.getElementById('IEfad1').style.top  = '10px';
    document.getElementById('IEfad1').style.left = '10px';
}

Do this:

if (DOM) {
    var elm = document.getElementById('IEfad1');
    elm.innerHTML = TopnewsStr;
    elm.style.top = '10px';
    elm.style.left = '10px';
}

Getting into the habit of saving references to objects rather than looking them up each time is a Good Thing (tm).

Now that you can manipulate positioned elements you would typically encapsulate your code in external files. This is where things get a bit strange. What we found is that newer browsers like NS6 and IE5.5 act differently when you use external JavaScript source files that are dynamically written. The next section will show this new behavior and some workarounds.

home / programming / javascript / netscape6 111156
[previous] [next]

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

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Review: Lenovo ThinkPad SL300 · OCZ PC3-10666 Gold 2x1GB Review · Apple Recommends Antivirus for Macs


Created: February 15, 2001
Revised: Mar. 6, 2001


URL: http://webreference.com/programming/javascript/javascript/netscape6/