Detect OS and browser

I was looking for a script that can detect your OS and browser and depending on that would do different things.
To be exact a link for download, in windows would dowload an .exe file and in mac would open a flash version of it.

Hey there,

I have used this code for browser and it works fine:

if(navigator.appName !== “Microsoft Internet Explorer”)

I googled this for O.S.

var OSName=“Unknown OS”;
if (navigator.appVersion.indexOf(“Win”)!=-1) OSName=“Windows”;
if (navigator.appVersion.indexOf(“Mac”)!=-1) OSName=“MacOS”;
if (navigator.appVersion.indexOf(“X11”)!=-1) OSName=“UNIX”;
if (navigator.appVersion.indexOf(“Linux”)!=-1) OSName=“Linux”;

Have never used it myself so you will have to test it :slight_smile:

ok, but how do i implement it (i don’t know much about javascript).

could you give me the full code so i can test it. I know how to edit javascript and merge javascripts, so they’ll do what i want. But to write it from scratch is’nt my cup of coffee.

Hey there,
Just give me a few hours, I am a developer and I am in work so I will locate the scripts for you and upload a zip,
kind regards

Will Edwards

Wow, thanks for help, I will be waiting :slight_smile:

Hey Barrio,

Here is some code I wrote ages ago to select different windows media player videos. It users javascript to discover whether or not the browser is IE and depending on whether it is, it embeds different player code (using the inner.HTML function.) I am sure that you can hack it to do something useful with a bit of research and patience.

Let me know how you get on.

Thanks silversurfer5150, i will try it as soon as the attachment gets approved :slight_smile:

Of course whatever code you use is only going to work part of the time since it is referencing free format user enterable data that can contain anything at all. What are you going to do when someone is using IE or Firefox has it set to identify the browser as “None of your business what browser this is” on an operating system of “Windows Linux 4000”.

How many people really do that? I’ve never known anyone to do it.

Isn’t it better to use the media query min-device-width instead?

Hey mPaulino,

What is that? I’m not familiar with it, but then again I’m far from an expert.

Not an expert either :stuck_out_tongue: It’s just based on experience (well, my developer office mates) it’s getting harder to target specific browsers in specific mobile platforms. I’m very bad in explaining so I’ll just provide you posts/presentations that discusses the pros and cons. I personally have yet to use it, just planning to on my personal project.

http://www.w3.org/TR/css3-mediaqueries/

http://www.cloudfour.com/css-media-query-for-mobile-is-fools-gold/
http://www.slideshare.net/bryanrieger/rethinking-the-mobile-web-by-yiibu

Opera can change the useragent between Internet Explorer, Firefox and Opera without the person using it even knowing that it is happening. There are probably lots of other browsers that do the same where sites stupidly try to test for specific browsers using the useragent so the browser automatically gives them the one they expect rather than the one that tells it which browser is really being used.

The useragent field should never be relied on for anything other than statistics where it will at least give approximately correct results (at least for the major browsers).

using the useragent to detect the browser and version is ok if you are only supporting the main/most popular browsers like IE, FF, Opera, Safari and Chrome.

Afaik, none of the above try to mimmick another browser as they have their own individual user agent.

If an unsupported browser mimmicks one of the browsers I support and a web page ends up not displaying properly in that unsupported browser then it doesn’t concern me at all because I don’t support that browser in the first place.

If an unsupported browser that doesn’t mimmick one of my supported browsers requests one of my web page, it gets assigned the FF3 stylesheet and I hope for the best - but with no guarantees :wink:

There are far too many browsers out there for me to give an unconditional 100% guarantee that my web pages will render correctly in all of them.

I’m doing something wrong, it opens the first flashlink at all times.

function load() {
       if(navigator.appName !== "Safari")
        		{
			open('FLASHLINK');
            }
          	else if(navigator.appName !== "Mozilla Firefox")
			open = ('EXEVERSION');
          	}


Opera is actually rather famous for its setting of different user agents. Especially since the Bork Incident : ) Microsoft actively tried to give Opera users a crappy broken page even though Opera was able to display the page just fine. Since then, Opera came out with a useragent name of IE a lot of the time. Wells Fargo, a major server manufacturer (who was trying to get Opera as a client!), and Google have all blocked Opera at one time or another. The solution is to change the user agent. Magically, everything then works. I have also changed the user agent on one of my Firefoxes in order to access a page that was trying to block all non-Safari browsers. So I just called mine Safari. Done.

http://dev.opera.com/articles/view/a-browser-sniffing-warning-the-trouble/

http://my.opera.com/haavard/blog/2010/09/08/google-instant

http://my.opera.com/community/openweb/idopera/

More recently, when Opera started stating that it is Operax.x, the old crapalicious scripts that sniffed for browser versions puked on “Opera10”. Why? They expected only single digits before the decimal. This means “Opera 10” == “Opera 1” which of course is “too old”, and thus the user gets the message “your browser is too old for this page, please install Internet Explorer because it’s teh Awesome” or whatever.

http://my.opera.com/hallvors/blog/2008/12/19/10-is-the-one

so now they say 9.8 lawlz.

Normally, instead of useragent sniffing, one would do feature-detection… but in this case, you really do need to know at least the OS (not sure what the browser type has to do here).

A way around this is of course to offer the option for users to choose “.exe” or “.tar” or whatever. You could cover this up with Javascript, and have some exception where if the user agents/OSes you’re looking for aren’t matched, the user gets the non-JS page (where they can be smart enough to choose the correct file format… heck, you can even state above each option that “exe’s are for Windows” etc).

Okay, I think I’ve got it.
But I want pc users still to be able to download the exe version which means I want it to detect OS aswell so that only mac users with safari will have the flashversion opened.



function link() {
       if(navigator.appName == "Safari")
        		{
			open(FLASHVERSION');
            }
        else
			{
			open('EXEVERSION');
          	}
}


</Script>

Buut, uh oh! You can’t give Flash to Safari all the time… because the user might be on an iPhone or something. Those are also using Safari (they can also use Opera of course) so you’ll want to check for iWhatevers and give them some other file.

iPhone/iPad/iWhatevers’ browsers are certainly more careful with their OS names passed on by the user agent, esp knowing a lot of developers are either targeting them or specifically NOT targeting them.

And what if I’m using Safari-for-Windows?

I think you’ll want to go by OS alone.

Also, what are you offering Linux users? (did I miss it?)

Well yes Linux users should have flashversion opened aswell, but I want it to be detectable by OS probably.