Jump to content

Browser Version


pritam79

Recommended Posts

<html><body><script type="text/javascript">var browser=navigator.appName;var b_version=navigator.appVersion;var version=parseFloat(b_version);document.write("Browser name: "+ browser);document.write("<br />");document.write("Browser version: "+ version);</script></body></html>

This code gives the browser name and version of my browser. I am using Mozilla firefox version 3.0, but the output I am getting is-Browser name: NetscapeBrowser version: 5Why is it so?Again using another piece of code as below-

<html><head><script type="text/javascript">function detectBrowser(){var browser=navigator.appName;var b_version=navigator.appVersion;var version=parseFloat(b_version);if ((browser=="Netscape"||browser=="Microsoft Internet Explorer")  && (version>=4))  {  alert("Your browser is good enough!");  }else  {  alert("It's time to upgrade your browser!");  }}</script></head><body onload="detectBrowser()"></body></html>

I get the output “Your browser is good enough!”. But according to the “if” condition neither my browser if “Netscape” nor the version is “>=4” as stated in the code. Please help.

Link to comment
Share on other sites

Firefox shows Netscape because Mozilla was developed by the people who made Netscape.Attempting to detect a browser is not good. People used to use the "version 4" method when Firefox first came out, except that they were retrieving the version from the UserAgent string. Firefox was version 1.5 at the time, but the codes would say "Your browser is not capable" because it wasn't version 4 or higher. Firefox 1.5 was most certainly capable of quite a lot more than most other browsers at that time."Browser sniffing," as it's called, is a bad practise as a programmer. If there's a feature your application uses, then test for the feature and if it doesn't exist then send a message to the user.A simple example:

If (document.getElementById) {  // Do something} else {  alert("Your browser is very old and does not support the necessary methods")  throw(new Error("Your browser is very old and does not support the necessary methods"));}

Link to comment
Share on other sites

"Browser sniffing," as it's called, is a bad practise as a programmer. If there's a feature your application uses, then test for the feature and if it doesn't exist then send a message to the user.
This would make me a "bad programmer", and I am a little embarrassed. I would like to think of myself as a "good" beginner.A while back I created a mini program that sniffs only for the browsers and minimum versions for which I test my webpages -- not because I do not agree with the above statement; rather, because I do not know what features my browsers require or do not require to run my pages.Although I do not deny entry to my pages with different browsers, I do slow entry and encourage my viewers to use the browsers for which my pages were tested. Of course, I am not running a commercial establishment either.Try viewing the following page using IE or past versions of Firefox and Safari and see what happens.http://homepage.mac.com/moogoonghwa/Imagine_Prototype/What I learned in creating my program was that browser manufacturers do not all use the same code, and that you must be careful about what you sniff for.Roddy
Link to comment
Share on other sites

Attempting to detect a browser is not good. ..."Browser sniffing," as it's called, is a bad practise as a programmer. If there's a feature your application uses, then test for the feature and if it doesn't exist then send a message to the user.
I strongly disagree, and if you want highly consistent applications, so should you.Pretending browsers are the same (when they clearly are not) is almost sure to cause layout quirks and unexpected results.Feature detection is great in theory, but the mere presence of a feature tells you nothing about its compliance.for example, table.rows[0] refers to the first row from the tbody in all browsers except Opera, where it returns the first <TR>, even if it's in the thead.as another example, onkeypress and onkeydown report (and fail to report) different things in different browsers, even though they all support the same features.when you start delving into dynamic CSS manipulation, browser sniffing is a flat-out must due to implementation variations.for example, overflow="auto" defaults to "scroll" in firefox, but "visible" in Safari when too much content is contained.this is a major bummer since there is no way to tell ahead of time when/if something will be/is fixed.good programmers will use whatever tools and information are available to produce the behavior they desire.
Link to comment
Share on other sites

I strongly disagree, and if you want highly consistent applications, so should you.Pretending browsers are the same (when they clearly are not) is almost sure to cause layout quirks and unexpected results.Feature detection is great in theory, but the mere presence of a feature tells you nothing about its compliance.for example, table.rows[0] refers to the first row from the tbody in all browsers except Opera, where it returns the first <TR>, even if it's in the thead.as another example, onkeypress and onkeydown report (and fail to report) different things in different browsers, even though they all support the same features.when you start delving into dynamic CSS manipulation, browser sniffing is a flat-out must due to implementation variations.for example, overflow="auto" defaults to "scroll" in firefox, but "visible" in Safari when too much content is contained.this is a major bummer since there is no way to tell ahead of time when/if something will be/is fixed.good programmers will use whatever tools and information are available to produce the behavior they desire.
The correct way to access table rows is through DOM methods:
var tableElement = document.getElementsByTagName("table");var tableRows = tableElement.getElementsByTagName("tr")

I just tested the overflow property in Safari. The box behaves as expected: a scrollbar appears when the content exceeds the box height.overflow: auto doesn't default to anything but itself. The scroll value would have both horizontal and vertical scrollbars if they were needed.The problem with browser detection is that browsers are constantly changing. When Internet Explorer 7 came out, half of the websites didn't work on it due to browser detection. The same happened with the release of Internet Explorer 8. I have Javascript applications with hundreds of lines and I never need to use browser detection.If you want an application to work correctly, you have to test for the features that you intend to use and their alternatives, rather than which browser is using them.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...