Treehouse

Detecting Google Chrome Using JavaScript

By on  

As we all know, Google's Chrome browser hit the web yesterday. Whether you wanted to or not, you definitely downloaded Google's effort to see what ingenuity they put into their browser. It's clean, fast, and more stable than Firefox has been for me lately. Of course, there's always a chance that Chrome will have a quirk that none of the other browsers have and you may need to use client side detection to fix the problem. You can detect Chrome by using the following bit of JavaScript:

var is_chrome = window.chrome;

The following string represents Chrome's full user agent:

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13

Have any Chrome-specific functionality you want to build? Maybe a MooTools slide-in script that says "Hey, you're using Google's new browser!" It's probably too soon to try to fix Chrome-related bugs, but knowing how to detect it sooner rather than later is a good thing.

ydkjs-6.png

Recent Features

Incredible Demos

  • Create a Spinning, Zooming Effect with CSS3

    In case you weren't aware, CSS animations are awesome.  They're smooth, less taxing than JavaScript, and are the future of node animation within browsers.  Dojo's mobile solution, dojox.mobile, uses CSS animations instead of JavaScript to lighten the application's JavaScript footprint.  One of my favorite effects...

  • JavaScript Battery API

    Mozilla Aurora 11 was recently released with a bevy of new features. One of those great new features is their initial implementation of the Battery Status API. This simple API provides you information about the battery's current charge level, its...

Discussion

  1. Thanks for sharing this. I hope there won’t be any Chrome-specific fixing :)

    • hi ,
      but i must say there is if you need to detect the file type and special characters in a file name while uploading the file you would need to detect FF IE6 IE8 and CHROME SPECIFICALLY as when you try to get the value of the file type field in javascript they would return diff results for all these browsers FF returns filename only and IE6 & CHROME returns FILE PATH

    • krukid

      there’s also the issue with fading out a textarea (or div) with a visible scrollbar – chrome fills it with rainbow colors

  2. Thank you! Very clever!!!

  3. Chrome crashed a couple of times of by now.

    Keeping FireFox for now, Chrome does have some nice fresh ideas like e.g. the way it separates various threads.

    Thank you for the js-tip!

  4. Martin

    It’s a nice idea. The only problem is that Google Chrome have exactly the same userAgent string as Apple’s Safari (at least on the Windows platform). I was trying to find out if there is anyway to tell the difference between them by searching the web and that’s how I ended up on your nice blog.

    • Per

      That is the same way I ended up here Martin.
      I was writing a JavaScript to detect browser and when my friend (with Chrome) tested the link we figured out that he was detected as Safari…. :-(
      Did you find an answer to find a way to fix that?

  5. As it’s still beta there may be one or two bugs, none that I have found so far, but I can’t imagine Google signing off on a product that didn’t fully adhere to standards. So maybe it will be the fixes we’ve had to implement for other browsers that cause issues in Chrome.

    Since Chrome was built to handle the next generation of web applications I can see us developers needing code to detect chrome in order to allow more functionality to be shown that legacy browsers maybe cannot handle and which is turned off or hidden by default.

    Anyway, thanks for the code snippet, though hopefully I’ll never need it.

  6. Thanks a lot for sharing..! But i still have no idea what should we fix in Chrome =D

  7. Chrome appears to pass Acid2, but does not pass Acid3 (linktest failed, and only 79 scored for me on Vista Home Premium). So there may be some issues, particularly when using JS libraries.

  8. i keep learning about more and more advantages and features with Chrome, with privacy, for example; now if only they would take care of it’s cookie management glitches…

  9. http://browserspy.dk/googlechrome.php will also show you the version and some additional information about Google Chrome.

  10. chrismarx

    I’m having trouble with a custom cursor (.cur file). chrome doesn’t appear to be respecting the correct hotspot set on the cursor. ff/ie have no problem.

  11. @chrismarx: Interesting, I hadn’t heard of that issue.

  12. chrismarx

    looks like they might be on to the problem, check it out:

    r2406 Fix an issue with tooltips showing up in the wrong location.

    http://dev.chromium.org/getting-involved/dev-channel/release-notes

  13. sling

    CSS margin tag doesn’t work with a percentage value in chrome.
    eg.
    .class{margin:10% 15%} doesn’t work

  14. Chrome doesn’t always handle events that IE and Firefox consistently do. For example:

    HTML

    Javascript

    function menu2Down(e){
    findTarget(e)
    …. }

    function findTarget(e){

    var targ;

    if (!e) {var e = window.event }

    if (e.target){ targ = e.target; }

    else if (e.srcElement){ targ = e.srcElement; }

    Perhaps you could check it out with a simple example

  15. You can also use this JavaScript library to detect any browser including chrome. Not only this, you can trim a string, detect mouse positions etc

    http://rochakchauhan.com/blog/2008/10/11/rochakjs-javascript-class-of-common-functions/

  16. Thanks for that! It’s been a great help! I’VE FOUND ANOTHER GOOGLE CHROME BUG:
    (sorry, didn’t mean to shout, was just to grab your attention!) lol.
    Overall I love Google Chrome, but one thing has just come up that I thought would be useful to point out: Google Chrome doesn’t respond to XML in the same way (as I’ve just found out!) Both IExplorer and Firefox use different methods to acheive this, and usually Google Chrome is easy to deal with because from a javascript point of vew, has behaved as though it was a clone of Netscape/Mozilla/Firefox! HOWEVER…
    BEWARE THE DANGERS OF CODING THEM THE SAME, as your browser will just assume you’re using Firefox, and anything that Google Chrome does not do, will go by undetected. No Warnings, No Nothing! (Which is fine just so long as the code it’s skipping wasn’t majorly important) but generally speaking, if you’ve gone to all the trouble to code it in XML, then there’s a likelyhood that the code it has skipped is VERY IMPORTANT INDEED, and thus losing it could be disasterous! However, thankfully this handy Google Chrome detection will help me to further seperate the code being used, and I can get to work on figuring out how the google has messed with the XML part! …Wish me luck!!

  17. Just downloaded chrome its blazing fast almost as fast as FireFox Beta 3 “hacked”.

    Just wanted to give everyone a heads up that When it comes to Frames or basically anthing that you resize with Javascript, that Chromes height variable is typically off by about 25px, however this just might be because of programing a resizing frame for all 3 browers. Because I checked the variables with Chromes nifty Inspector tool and the clientHeight variable is exact. So im guess it’s just another incompatability of IE to any other browser again. Altho IE still does nice color shading and easy margins lmao. =D.

  18. James Umokoro

    Chrome is very nice.. but it would be nicer if it could show error messages like that that of the Windows Internet Explorer. Or may be should have a debugger. Guys don’t you think it will make good sense? Love the work these guys have done.

  19. Sameer

    Browser detection is fine how to change the input type using js in chrome.

  20. Chrome is nice, but still rather unstable I think

  21. Mis_P

    @Martin: did you find a way to differentiate user agent for safari and chrome..because I am trying to extract the “chrome” from the user agents long string but still it does not work.

    • Per

      i_chrome = navigator.userAgent.toLowerCase().indexOf(“chrome”) > -1;

  22. Hi that is a great news for all people like me….I don’t know a lot about wave and thus that is very new for me…but that is a very very good news for me because collaboratively I can use their service along with twitter and many other features at a time that is nice…thanks Google and thanks web master of this site..

  23. Rhoel

    @Htoo Tay Zar:

    i have discovered the interpretation of the Khmer (Cambodian) Unicode font is tiny in Chrome when compared to FF and IE. So maybe a need to script around to make the font size readable.

    Not a big issue.

    Do like Chrome though – can’t wait to see the roll out on Chrome OS

    Rhoel
    Phnom Penh, Cambodia.

  24. Thanks again david :) I didn’t know it was so easy to detect a browser via javascript…

  25. LaQuisha Redfern

    OMG – you just saved my coding life – thank you!

  26. @mis_p i know u posted an age ago…. but, if you check for chrome before you check for safari then the you chrome will come up,

    I had the same issue myself, :) hope it helps anyone who sees this thread

  27. Look forward to the day that font-face works decently in Chrome. Getting close but flakes out in the first dozen fonts I tried. Gave up. Shame b/c all the other browsers could render the fonts.

  28. Why does Chrome’s User Agent string have Mozilla’s name on it?

  29. Sorry to burst your bubble:

    if(window.chrome) { alert("You are using chrome"); }
    else { alert("You are not using chrome"); }

    • gwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiotgwt lost idiot

  30. Hi!.
    I am using a object tag to show a webpage inside another page.
    in object tag i show some images,when we click on image a pop div is created and it is appended to the parent page body by using
    javascript parent.docuemnt.body function.but the pop up is only working in IE and Mozilla.I wan it should run in chrome & safari..
    Plz suggest any help………….

  31. Great tip to detect Chrome. Thanks for sharing.

  32. This is what I did:

    var chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
    alert(chrome);

    It will return “true” if you’re using chrome browser. Thanks for sharing this wonderful script!

  33. Solgin

    Thanks for sharing.This works for me
    var chrome = navigator.userAgent.toLowerCase().indexOf(‘chrome’) > -1;
    alert(chrome);

  34. Laci

    This is easy!:) That would help me if you could point me out how to detect if chrome runs in –disable-web-security or not… As I have a documentation written in HTML with a js search engine – that is forbidden by Chrome as it has interframe js thing… so I would like to make an alert box for those using chrome without this –disable-web-security parameter to be aware of this problem (not the searchbox is not working but chrome is disabling it)

  35. Rob

    I can’t have multiple versions of all my video files (there’s lots and the whole site is downloaded) so I wrote this and it seems to work on current Safari, FireFox and Chrome -should work on everything else or add to the non MP4 list

    /*U might need to add other non-MP4 native browsers*/
    if (navigator.userAgent.indexOf(‘Firefox’||’Chrome’)!= -1) {
    document.write(“Sequence 03“)
    }
    else{
    document.write(” “)
    }

  36. Rob

    I can't have multiple versions of all my video files (there's lots and the whole site is downloaded) so I wrote this and it seems to work on current Safari, FireFox and Chrome -should work on everything else or add to the non MP4 list

    /*U might need to add other non-MP4 native browsers*/
    if (navigator.userAgent.indexOf('Firefox'||'Chrome')!= -1) {
    document.write("Sequence 03")
    }
    else{
    document.write(" ")
    }

  37. Rob

    /*U might need to add other non-MP4 native browsers*/
    if (navigator.userAgent.indexOf('Firefox'||'Chrome')!= -1) {
    document.write("Sequence 03")
    }
    else{
    document.write(" ")
    }

  38. Rob

    aargghh – can’t post the code

  39. Thanks for the code. My script works for everything but for chrome. It is not redirecting properly. This code helped me fix and handle chrome different.

  40. Better watch out or you’ll hit on “chromeframe” in the useragent string if you’re just searching for “chrome” like that. a little safer to check for “chrome/” i think.. or “chrome” and not “chromeframe” (both).. because even though chromeframe may be in the useragent, it doesn’t mean it’s currently active.

  41. […] Detecting Google Chrome Using JavaScript […]

  42. […] Detecting Google Chrome Using JavaScript […]

  43. This doesn’t happen genuinely matter a great deal, in addition to getting the concept progresses every thing kind of pulls collectively.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!