Easy Client-Side Sniff and Divert Script for Personal Sites (version 3)


What does it do? Test it!
Put into a web page, it would either change the location to yourNormalPages/URL.htm, or divert to yourFallBackUponURL.htm, or do nothing.

What do I have to do?
You should select and copy the script code below, and paste it into the HEAD section of a 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-personal -->
<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 Dir2 = 'yourNormalPages/URL.htm' //coding for modern browsers
var Dir2Gen = 4 //Supposed browser generation for the NormalPages: 4 or 5 (default: 4)
//DO NOT CHANGE ANYTHING BELOW THIS LINE *********
var pardonvillkoret='navigator.examined.w3cdom && navigator.appCodeNameSub >='+Dir2Gen;
function UseStatic(){ window.location.href = fbURL; }
function Relocate(l,d){(l=='HS')? location.replace(d): location = d;}
function CheckStatic(){Relocate(Dir0,fbURL);}
function UseDynamic(){ Relocate(Dir0,Dir2);}
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=eval(pardonvillkoret); (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 some information within NOSCRIPT tags:

 <noscript>
    yourMessageForBrowsersNotJavaScriptEnabled
                        </noscript>

Now, as you might have noted already, there are two 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. 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 examine the browser's document object model, and compare the results with the number variable stated in the HEAD code's Dir2Gen (cp. above). If the value of the current browser is equal to or higher than the one stated, then the visitor will be relocated to yourNormalPages/URL. Seeing that Internet Explorer 6 returns 5 for browser generation, it doesn't matter whether you change the default value; that browser is always welcomed onto your site. Otherwise, it would have been diverted to yourFallBackUponURL, which indeed Internet Explorer version 5 would go, if you changed the requirement from 4 to 5 (thereby disregarding browsers not up to current standards). In fact, the even older Netscape Communicator and Internet Explorer version 4 will both be diverted to your fallback location no matter what.

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..

For more information about the Script files, go to the Demonstration page.
This script is FREE to be used but NOT to be traded in.

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.


top of page