What does it do? Test it!
Put into a web page, it would either change the location to yourNormalPages/URL.htm or
yourSimplePages/URL.htm, or divert to yourFallBackUponURL.htm, or do nothing.
What do we have to do?
You should select and copy the script code below, and paste it into the HEAD
section of a
(prominent) web page on your site. A few lines down the code, there is a link to a script source file
uasniff.js
, which you should download and save as is, to the
same directory as the web page, or anywhere on your site.
<!-- JavaScript sniffing begins. Change the faked destination URL's and some optional codings. The Client-Side Sniff and Divert Script. Code version $3.0.0-business --> <script type="text/javascript"><!-- function stopError(){return true}; window.onerror = stopError; // --></script> <script type="text/javascript" src="uasniff.js"></script> <!-- (C) lars.pm 2001-2005 peemm@chello.se This is author info. Please do not remove. --> <script type="text/javascript"><!-- var fbURL= 'yourFallBackUponURL.htm' //no support message for dated browsers var Dir0 = 'HS' //'HS': replaces the current page; 'HN': goes on to the next page var Dir1 = 'yourSimplePages/URL.htm' //coding for older or less advanced browsers var sniffAction = 'pardon' //remove this keyword to allow DHTML/ECMA browsers only: = '' var Dir2 = 'yourNormalPages/URL.htm' //coding for standards compliant advanced browsers var Dir2DOM = 'w3cdom' /* requisite DOM compliance for the NormalPages: 'w3cdom' (default) * or 'domelementstyle' or 'domcreateevent' or 'msieapi' */ //DO NOT CHANGE ANYTHING BELOW THIS LINE ********* var exact2villkoret='navigator.examined.'+Dir2DOM; function UseStatic(){ window.location.href = fbURL; } function Relocate(l,d){(l=='HS')? location.replace(d): location = d;} function CheckStatic(){(sniffAction == 'pardon')? Relocate(Dir0,Dir1): Relocate(Dir0,fbURL);} function UseDynamic(){ var e2=eval(exact2villkoret); if (e2 && (navigator.appCodeNameSub >= 5)) Relocate(Dir0,Dir2); else Relocate(Dir0,Dir1);} function Init(){var n=(window.navigator != null? window.navigator:null); if (!n || ((n.appName=='Netscape'&& parseInt(n.appVersion)<3) || (n.userAgent.indexOf('MSIE')!=-1 && parseInt(n.appVersion)<4))){ UseStatic();} else if(typeof(Exami)=='function') { Exami(); var pv=(navigator.examined.ecma); (pv)? UseDynamic(): CheckStatic();} else alert('PM QA\: Error! Missing JavaScript file\(s\).');} // THE SCRIPT ENDS HERE. (C)PM 2001-2005. FREE FOR USE (NOT FOR TRADE). --></script> <!-- JavaScript sniffing ends. Visit http://members.chello.se/peemm for killing time only. -->
In the BODY
tag of the web page, you need to put an onload event handler to initiate
the script, or you could use any event handler to call the Init function (but do not call
Init until the document is fully loaded.):
<body onLoad="Init()">
So, what else?
For the case the script will do nothing, i.e. JavaScript is not enabled or the
browser hasn't implemented the script language, you would need to provide the web page content within
NOSCRIPT
tags. Preferably this web page should be written using HTML3.2 or HTML4
Transitional code. To facilitate maintenance of the site, I suggest use of the same code
here as on yourSimplePages:
<noscript> yourWebPageContentForBrowsersNotJavaScriptEnabled </noscript>
Now, as you might have noted already, there are three placeholder URI's in the HEAD
script code, which should be replaced in
accordance with the site's paths, and there is also the possibility of optional modifications to the script
variables. Then, you're done :-)
OK, but what..?
...is happening? Let me adopt a scenario. A visitor to your site is using Internet Explorer,
version 6. The script is handling the occasion like this; first, it'll analyze the values of some
navigator properties to identify the browser properly. It is also checking occurences of
some key scripting implementations. All this is done already while the page is still beginning to load. Second,
with Init(), the script will divert the visitor to yourFallBackUponURL if a known
JavaScript 1.0 browser is used. (We are talking about 10-year-old browsers here, so the chance is
rather small, but not improbable.) Third, the script will go back to its earlier findings about scripting
implementations and see whether the browser is ECMAScript (edition 1)
compliant or not. Additionally it's checking the value of the sniffAction variable stated in the
HEAD
code. (cp. above). Now, if the browser wasn't script compliant
yourSimplePages/URL would have been loaded by default. However, were the sniffAction
variable's keyword "pardon" removed, the browser would've been diverted to yourFallBackUponURL.
Although, in my example scenario, neither outcome would have been effectuated.
Internet Explorer 6 returns true
for ECMAScript compliance. It means it
might be welcomed to yourNormalPages/URL. It depends on the variable Dir2DOM
stated in the HEAD
code. (cp. above). The script has already found out about the
Document Object Model (DOM) the browser is applying, and since Internet Explorer
6 has implemented relevant parts of the W3C DOM (the
variable value "w3cdom"), that browser is clear for yourNormalPages/URL. It would even have been
clear if the Dir2DOM value had been changed to either "domelementstyle" or "msieapi". Had, on the contrary,
"domcreateevent" been set as a requirement, Internet Explorer 6 would have loaded
yourSimplePages instead, because that browser doesn't handle events according to the
(DOM) standards.
The last thing happening is the use of the HEAD
code's Dir0 value to decide on the
relocation method. If you'd like the Script to try (not always possible) to apply the
location.replace("SomeURL")
method, thereby causing no extra entry
into the history sequence, you should leave the value as it is by default. This would be useful in
the case of initiating the script on an onLoad
event, then saving the functionality of the
Back button. If you rather need the script to apply the location =
"SomeURL"
method, when diverting the visiting browser to another location,
you should change the value to "HN". That would be useful in the case of initiating the script on an
onClick
event, thereby marking another entry into the history sequence..
Disclaimer: Please, notice that although the script code is believed to work with all current and correctly working JavaScript browsers, it should be applied with care. There is NO WARRANTY of its proper behaviour, and it is offered to the public AS IS, the author refraining from admitting ANY RESPONSIBILITY WHATSOEVER for unexpected results when use is made of it, EVEN IF USED AS INTENDED. The author would REJECT ANY CLAIM for financial or any other loss supposedly because of it.