Sabtu, 26 Juli 2008

JavaScript Browser Detection

Browser Detection

Almost everything in this tutorial works on all JavaScript-enabled browsers. However, there are some things that just don't work on certain browsers - specially on older browsers.

So, sometimes it can be very useful to detect the visitor's browser type and version, and then serve up the appropriate information.

The best way to do this is to make your web pages smart enough to look one way to some browsers and another way to other browsers.

JavaScript includes an object called the Navigator object, that can be used for this purpose.

The Navigator object contains information about the visitor's browser name, browser version, and more.


The Navigator Object

The JavaScript Navigator object contains all information about the visitor's browser. We are going to look at two properties of the Navigator object:

  • appName - holds the name of the browser
  • appVersion - holds, among other things, the version of the browser
he variable browser in the example above holds the name of the browser, i.e. "Netscape" or "Microsoft Internet Explorer".

The appVersion property in the example above returns a string that contains much more information than just the version number, but for now we are only interested in the version number. To pull the version number out of the string we are using a function called parseFloat(), which pulls the first thing that looks like a decimal number out of a string and returns it.

IMPORTANT! The version number is WRONG in IE 5.0 or later! Microsoft starts the appVersion string with the number 4.0. in IE 5.0 and IE 6.0!!! Why did they do that??? However, JavaScript is the same in IE6, IE5 and IE4, so for most scripts it is ok.





Tidak ada komentar:

Clock "WIB"

welcome to my blog

selamat datang di blog saya!!!!!!!!
kemungkinan saya belum bisa banyak posting karena saya masih sibuk dengan urusan sekolah "persiapan UN" jadi sekitar 3 bulan kedapan baru bisa posting,,,,,
dan terima kasih sudah mengunjungi blog saya....
bagi anda yang berminat mengirimkan tulisan di blog saya silahkan kirim ke sini wira.sanj@gmail.com nanti akan saya posting!!!!