spacer

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

home / programming / javascript / operalies current pageTo page 2
[next]

When You Can't Trust the Browser

Developer News
Microsoft Shows Some Ankle With Visual Studio
Gentoo Linux Cancels Distribution
It's Official: Windows 7 at PDC, WinHEC

By Kenneth Tibbetts (postmaster@yankeeweb.com)

I like Opera--the browser, not the musical spectacle. I like that it puts small demands on your hardware. I like that it displays Web pages quickly and accurately. I like that it has always been close to the standards suggested by the W3C, especially for CSS styles.

It is always worthwhile to preview pages in Opera. Opera is a little weak in event handling, and it has been slow to adopt the document object model. But in terms of page presentation, Opera shows a Web page the way it oughta look. It displays a great looking page, and it loads fast. And everybody likes the little guy.

The troubles I have with Opera, as a code writer, are all derived from a single bad habit of the browser. Opera is a little loose with the truth.

In fact, Opera tells lies.

Little Lies

When I write code for Web pages, I write the page with the dumbest browser in mind, and then add scripts for the clients that can use them. I use a kind of a sniffer tool called iz to get the information from the browser.

Without using a tool like iz, you can pre-condition every script to ask if the client supports some particular property or method: image objects, or layers, or whatever.

If (document.layers) or (document.images) return true, the condition is supported, and you can pass on to the next step in your script.

Or you can directly query the navigator object for its appName or userAgent string, or test it against a string you know:

if(navigator.appName.indexOf('Microsoft') != -1);

or

if(navigator.appName== 'Netscape');

or

if(navigator.userAgent.indexOf('Gecko')!=-1);

In the old days I would see if the app's name was Netscape or Microsoft, and then check the version number, and that was it. Now I use iz.

iz source

iz deals with the "Other Guy" in the Web page: the user, the client, the browser--the software that is translating and displaying your code. It's best to cultivate an amiable relationship with this character.

As the page writer, you have no idea who or what this guy is and what he is capable of, but you can ask a few questions.

function iz(what){

 if(!what) return navigator.appName+'; '+ navigator.userAgent;
// no arguments passed get the return case sensitive

 var agnt= navigator.userAgent.toLowerCase();
// otherwize the uA is made lower case for comparisons

 var wot=what.toLowerCase();

 switch (wot){
  case 'df': return !!(document.createDocumentFragment);
  case 'dom': return !!(document.createElement);
  case 'id': return !!(document.getElementById);
  case 'ie': return !!(agnt.indexOf('msie')!= -1);
  case 'moz': return !!(agnt.indexOf('gecko')!= -1);

  case 'agt': return agnt;
  default: return !!(agnt.indexOf(wot)!=-1);
 }
}

iz Comments

If you use iz with no argument, as

x=iz();

x gets the value of the browser's name and userAgent string, separated by a semicolon.

IE6 returns:

Microsoft Internet Explorer; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

Netscape 6.2.1:

Netscape; Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:0.9.4) Gecko/20011128 Netscape6/6.2.1

Netscape Navigator:

Netscape; Mozilla/4.08 [en] (WinNT; U ;Nav)

Send it one of the constants (ID, DF, IE, MOZ and my favorite, DOM), and it returns true or false, depending on the results of the case statements that apply to the input string.

I take extra care with what gets returned from iz. I want to get a 'real' true or false, and not a null or an empty string. Prefixing !! to a condition test forces the true / false syntax to be returned.

iz('ID') tests for document.getElementById, and iz('DOM') returns true for document.createElement. IE and MOZ are shorthand for IE or a Mozilla browser.

Sending any other string, like iz('Mac') or iz('Opera') checks for the specified string in the userAgent string.

I ran up against Opera's little lie here--the user can present a fake id, claiming to be IE or Navigator. Your carefully crafted browser sniffer might as well be on break.


home / programming / javascript / operalies current pageTo page 2
[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
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
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
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
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
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data · Controllers: Programming Application Logic
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Sprint Launches Mobile WiMAX Network · Albatron Downsizes with the KI780G Mini-ITX Motherboard · Can't Find a Wi-Fi Network? Make Your Own.

Created: April 1, 2002
Revised: April 1, 2002

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