SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

  2. #2
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, but how do i implement it (i don't know much about javascript).

  4. #4
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  6. #6
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, thanks for help, I will be waiting

  7. #7
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

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

  8. #8
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks silversurfer5150, i will try it as soon as the attachment gets approved

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,819
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    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".
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  10. #10
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How many people really do that? I've never known anyone to do it.

  11. #11
    SitePoint Member mpaulino's Avatar
    Join Date
    Nov 2007
    Location
    Philippines
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Isn't it better to use the media query min-device-width instead?

  12. #12
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by mpaulino View Post
    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.

  13. #13
    SitePoint Member mpaulino's Avatar
    Join Date
    Nov 2007
    Location
    Philippines
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not an expert either :P 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.alistapart.com/articles/r...ve-web-design/
    http://www.cloudfour.com/css-media-q...is-fools-gold/
    http://www.slideshare.net/bryanriege...e-web-by-yiibu

  14. #14
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,819
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by silversurfer5150 View Post
    How many people really do that? I've never known anyone to do it.
    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).
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  15. #15
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    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.

  16. #16
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm doing something wrong, it opens the first flashlink at all times.

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

  17. #17
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Kalon
    Afaik, none of the above try to mimmick another browser as they have their own individual user agent.
    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://my.opera.com/haavard/blog/200...again-cars-com

    http://dev.opera.com/articles/view/a...g-the-trouble/

    http://my.opera.com/haavard/blog/201...google-instant


    Quote Originally Posted by Opera
    JavaScript navigator properties

    Note that if you use JavaScript to retrieve information about the browser, then it is advised to use the UA string and not navigator.appName and navigator.appVersion, because the application name attribute will show MSIE or Netscape if Opera spoofs or masks as IE or Firefox respectively. Other browsers, such as Firefox and Safari, will return Netscape by default, making these attributes unreliable for browser detection.
    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/20.../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).

  18. #18
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Code:
    function link() {
           if(navigator.appName == "Safari")
            		{
    			open(FLASHVERSION');
                }
            else
    			{
    			open('EXEVERSION');
              	}
    }
    
    
    </Script>

  19. #19
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    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?)

  20. #20
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well yes Linux users should have flashversion opened aswell, but I want it to be detectable by OS probably.

  21. #21
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm...that's interesting

    Now that you mention it, I now do recall that there were issues with Opera spoofing other browsers back in its early days. I am not sure if that is still an issue nowadays.

    I download all my browsers (IE, FF, Safari, Google and Opera) from the manufacturers' website so hopefully I am getting a legitimate version of the browser.

    When I output the navigator.useragent from my Opera v10 I get

    Opera/9.80 (Windows NT 6.0; U; en) Presto/2.2.15 Version/10.00
    My javascript looks for the string "Opera" in the useragent.

    If it finds it, it then looks for the string "Version/".

    If it finds it, I then have Opera v10+ and it does a .split() using "Version/" to get the version number.

    If it doesn't, then I have Opera 9- and I do a split() on "Opera" to get the version number.

    My javascript works fine on "legitimate" installations of the browsers I support.

  22. #22
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Sure but the issue is, people change them (sometimes they are encouraged to when a site blocks their browser). People are much less likely to change the OS version though.

    barrio: if you really want everyone to have access to this file, whatever it is, you'll want something as a default if the user doesn't have Flash. Since you're using Javascript for OS detection anyway, detect if they
    -have Flash
    -have a Flash player that's new enough (if version matters)

    Adobe has been a pain regarding Flash for Linux. I remember when we were all sitting around waiting for Flash 9 because the older versions worked terribly for Linux... back then, it was built only for Firefox. To get Opera and Chrome to also use the Flash plugin, you had to tell those browsers to look in the .mozilla folder. Later, Opera started shipping with that as the default action. Chrome I think started copying the .mozilla folder and Flash is in .chrome... I think. I'm not sure anymore, because I've not allowed Flash to come onto my current distro. I only have Flash on the Windows machines.

    Or, if that's too much work, you can just have the default be a message which says "this is a Flash file. In order to view it, you must have Flash Player version whatever".

  23. #23
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont want it to be super specific about stuff, I just want it to detect OS and browser that's it. I got it to detect the browser now I'm looking for the OS detection.

  24. #24
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    You might be able to use ppk's script from QuirksMode:
    http://www.quirksmode.org/js/detect.html

    The top one does OS detection (as well as some browser detection). However if you look at the page I linked to earlier (http://my.opera.com/community/openweb/idopera/), you'll see as you scroll down that if the user is on a mobile, the place where OS is usually listed (first in the ()'s) has other stuff like S60 for Symbian or J2ME/MIDP for phones running the Java virtual machine.

    But if you're only going for Desktop users right now, and are careful in updating your script for whatever other OSes you are looking for, try ppk's script out.

  25. #25
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stomme poes View Post
    Sure but the issue is, people change them (sometimes they are encouraged to when a site blocks their browser).......
    I'm starting to get a little confused as there seem to be a couple of sub-threads happening here but I assume the "them" is user agents fields.

    If that is so, then if someone changes the user agent in their browser I don't see it as an issue for me because I support only legitimate versions of browsers.

    So if a user changes the user agent in their browser and for some reason any of my web pages consequently do not render correctly then it's not my problem because they are using an illegitimate user agent for the browser.

    If the web page renders correctly with or without a legitimate user agent then all's well and good because no harm has been done from my point of view.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •