Imx Fix in my experience
 
« prev next »

June 25, 2003 11:55 AM


Ok, so this is weird, and I don't know why it was done this way (maybe Dave can explain), but Safari 1.0 returns the same value for the getYear() method that Netscape 4.x does, 103. Check this script out and then go ahead and click the button to see what it returns...
<script type="text/javascript" language="Javascript1.2">

function showYear() {
	var nowX    = new Date();
	var theYear = nowX.getYear();
	alert(theYear);
}

</script>
Try it:

So, you should have seen a modal dialog box with "103" in it (assuming you have your system clock set correctly), which is the current year, minus 1900 . That's how Navigator did the getYear() method. Why does Safari do that? There must be some (good? bad?) reason, and whatever it is, we still have to deal with it, and this is one way to go about it. First, we can do some browser detection for Safari like we do for Navigator 4.x...
var isMinNS4 = (navigator.appName.indexOf("Netscape") >= 0
                && parseFloat(navigator.appVersion) >= 4
                && parseFloat(navigator.appVersion) < 5) ? 1 : 0;
And we can do something similar for Safari by saying...
var isSafari = (navigator.userAgent.indexOf("Safari") >= 0) ? 1 : 0;
This basically says that the var "isSafari" is equal to "1" if the string "safari" occurs in the navigator.userAgent object. If it doesn't, then the value is 0, and later on we can use that value to say...
if (isMinNS4 || isSafari) { theYear += 1900; }
Which says that if it's Nav 4 or Safari then make "theYear" equal to itself plus 1900. And even us forensic JavaScripters should be able to figure out that 1900 + 103 = 2003. So, to get Safari to tell you what year it is, we can use this function...
<script type="text/javascript" language="Javascript1.2">

function showYear() {

	var isMinNS4 = (navigator.appName.indexOf("Netscape") >= 0
	                && parseFloat(navigator.appVersion) >= 4
	                && parseFloat(navigator.appVersion) < 5) ? 1 : 0;
					
	var isSafari = (navigator.userAgent.indexOf("Safari") >= 0) ? 1 : 0;

	var nowX    = new Date();
	var theYear = nowX.getYear();

	if (isMinNS4 || isSafari) { theYear += 1900; }

	alert(theYear);
}

</script>
Try it: