Object Sniffing New Browsers, The khtml Triumvirate: Part 1 | 2
Object Sniffing New Browsers, The khtml Triumvirate: Part 1
Surfin’ for Safari
Apple’s Safari is a new browser that was officially launched at the end of June 2003. It is worth knowing about since it is now the default browser bundled with the latest versions of OS X. So you can expect an increasing amount of Safari hits to your web sites from Mac users who have either recently upgraded their browser or who have bought new iMacs.
Like Konqueror, Safari is based upon the khtml rendering engine, honoring Mac OS X’s Unix roots. Since the khtml rendering engine is open source and was obtained by Apple via the GNU Lesser General Public License, any improvements made to it by the Apple’s development team means that any improvements to it will be incorporated back into khtml, and from there to Konqueror. This flow of information and improvements is not one-way, so further developments to Konqueror’s rendering engine from the open source programming community will ultimately benefit Apple’s Safari as well. The upshot of this is that over the long term, you can expect the rendering functionality and support for such things as CSS properties for both browsers to improve in tandem. Keep in mind though that “rendering engine” does not equal “browser”; Konqueror and Safari are very distinct browsers with their own set of features and ways of operating, but they will render Web pages in a similar fashion.
Much like Konqueror, Safari supports the document.childNodes object and supports neither document.all nor navigator.taintEnabled. It is distinguished from similar browsers by the fact that it doesn’t implement the somewhat obscure property accentColorName (used to specify the highlight color used for an active menu item) belonging to the navigator object. So you could use the following code to detect Safari:
- var is_safari =
(document.childNodes)&&(!document.all)&&(!navigator.taintEnabled)&&(!navigator.accentColorName)?true:false;
Another possibility worth further research is the fact that Safari is the only browser capable of running AppleScript applications. While there is a way to run JavaScript within AppleScript in Safari (using the “do JavaScript” command), I could not track down an object that would enable you to identify this functionality utilizing JavaScript – but if you could, you could positively distinguish this browser in tests.
OmniWeb is Owned
OmniWeb rounds out the triumvirate of modern browsers based upon the khtml rendering engine. Like Safari, OmniWeb 4.5 is also a Mac OS X browser, and one of the few remaining browsers that you have to pay for in order to use (though you can download and test-drive a demo version).
As you may have guessed by looking at the previous code examples, what distinguishes OmniWeb from Safari and Konqueror is that it uses navigator.accentColorName. So to distinguish it from the other two browsers, you could use the following code:
- var is_omniweb45plus =
(document.childNodes)&&(!document.all)&&(!navigator.taintEnabled)&&(navigator.accentColorName)?true:false;
When you put all of this code together, you have a nice little routine that will help you detect and sort out the khtml trio of browsers. Drop the following code segments into your favorite browser-sniffing code to be able to distinguish between these three khtml-based browsers:
<script>
...
- // khtml triumvirate
var is_khtml = (navigator.vendor == 'KDE')?true:false;
var is_konq = (navigator.vendor == 'KDE')||(document.childNodes)&&(!document.all)&&(!navigator.taintEnabled)?true:false;
- var is_safari =
(document.childNodes)&&(!document.all)&&(!navigator.taintEnabled)&&(!navigator.accentColorName)?true:false;
var is_omniweb45plus =
(document.childNodes)&&(!document.all)&&(!navigator.taintEnabled)&&(navigator.accentColorName)?true:false;
</script>
<body>
…
- document.write("<P>khtml:" + is_khtml + "<BR>");
document.write("safari:" + is_safari + "<BR>");
document.write("konqueror:" + is_konq + "<BR>");
document.write("omniweb:" + is_omniweb45plus + "<BR>");
</body>
Compiled by Keith Schengili-Roberts
The writer wishes to acknowledge the work of Mark Wilton-Jones and his browser sniffing tutorial at http://www.howtocreate.co.uk/tutorials/jsexamples/sniffer.html, to which this article is indebted.
Created: March 27, 2003
Revised: July 12, 2003
URL: URL: http://webreference.com/programming/javascript/sniffing

Find a programming school near you