Reply to Re: Stopping 'Submit' by Return Key

Your name:

Reply:


Posted by Michael Winter on 12/11/05 14:48

On 11/12/2005 01:35, Jonathan N. Little wrote:

[snip]

> If browser detection is needed

And when do you think that is?

One of the few times it is useful is when attempting to pre-empt issues
with the object model that cannot be detected by observing behaviour,
but this is rare, so much so that I can't even think of an example at
the moment.

> I use object detection not by application.

What you've shown below is object inference, and it is just as
inadvisable and unreliable. Object detection, better known as feature
detection, is entirely different.

> var NN4x=typeof(document.layers)!='undefined';

Netscape 4 is not the only browser to implement the layers collection.

> var MSIE=typeof(document.all)!='undefined';

Internet Explorer is not the only browser to implement the all collection.

> var DOM1=typeof(document.getElementById)!='undefined';

Support of the document.getElementById method is not indicative of
complete support of W3C DOM Level 1. Besides, DOM Level 1 is rarely used
on its own.

> [...] test for the object or functions existents is more reliable.

Indeed, and this is the approach that feature detection takes.

The browser (Fx, Op, IE, etc.) is irrelevant. One does not need to know
what it is in order to script it, and there is no way that any author
will know of every scriptable browser and its capabilities, so this
approach is already adding a completely unnecessary restriction.
Moreover, most browsers change over time, and trying to lump browsers
into groups and treating them exactly the same can lead to problems in
the future when a browser stops acting in the way you previously thought
it would, requiring constant revisions to the script.

What is relevant is what a host can do; what it's features are. If a
script will want to dynamically create an element then append it to a
specific element within the document tree, then the script should test
for each feature. Scripts should also be written defensively (just as
other programming disciplines demand) to cope gracefully with unexpected
failure conditions.

/* We'll need to create an element, and we'll need
* a reference to the element that will receive
* this new addition, so start by testing for them.
*/
if(document.getElementById && document.createElement) {
/* Now, we get our reference, create the new
* element, and check that we will be able to
* append it.
*/
var ref = document.getElementById('myElement'),
obj = document.createElement('div');

if(ref && obj && ref.appendChild) {
/* Finally, we append the element. */
ref.appendChild(obj);
}
}

Though longer than the blind approach that could be taken, this will not
be subject to fatal errors, a sure sign of incompetence.

This is a trivial example and only an overview of feature detection. The
latter has been discussed at length in c.l.javascript (see the Google
Archives[1]) and a longer description can also be found in the group's
FAQ Notes[2], under the title "Browser Detecting (and what to do
instead)"[3].

Mike


[1] c.l.javascript @ Google Groups
<http://groups.google.co.uk/group/comp.lang.javascript>
[2] c.l.javascript FAQ Notes
<http://www.jibbering.com/faq/faq_notes/faq_notes.html>
[3] Browser Detecting (and what to do instead)
<http://www.jibbering.com/faq/faq_notes/not_browser_detect.html>

--
Michael Winter
Prefix subject with [News] before replying by e-mail.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация