How to Get Browser Name and Version via JavaScript
Today I ran into a strange issue where Firefox version 28 and below rendered style widths different than Firefox 29 and above. Firefox 29 and above appear to have fixed the issue and render sizes to match Chrome/IE8+/Opera/Safari. Unfortunately, as old as Firefox 28 is, our client’s legal review team is stuck on that version as IT refuses to let them upgrade. As such, we needed to add a kludge fix to the site to add a style to fix the issue for those running older Firefox versions. JQuery removed the version support from version 1.9 so here’s a handy script that will allow you to detect the browser and version without any extra dependencies.
function get_browser_info(){ var ua=navigator.userAgent,tem,M=ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; if(/trident/i.test(M[1])){ tem=/\brv[ :]+(\d+)/g.exec(ua) || []; return {name:'IE ',version:(tem[1]||'')}; } if(M[1]==='Chrome'){ tem=ua.match(/\bOPR\/(\d+)/) if(tem!=null) {return {name:'Opera', version:tem[1]};} } M=M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?']; if((tem=ua.match(/version\/(\d+)/i))!=null) {M.splice(1,1,tem[1]);} return { name: M[0], version: M[1] }; }
Usage is very simple:
var browser=get_browser_info(); console.log(browser.name); console.log(browser.version);
BONUS: If you need to detect a specific version and add special classes, here’s a quick snippet that will allow you to add a class to the HTML tag using plain old vanilla.js.
var browser=get_browser_info(); if(browser.name == 'Firefox' && browser.version <= 28) { var root = document.documentElement; root.className += " firefox28"; }
Firefox Plumber Eliminates Memory Leaks
If you’re a power user like me, you’ll find Firefox will often balloon to about 1 gig of RAM usage after some time. Often times it’ll hit that point and then just crash.
I’m probably in the minority but my surfing habits tend to be open a mess of tabs and then come back and read later. I never go back to bookmarks (even though I do have a delicious account and actually tag stuff). At any time, I may 25-100 tabs open in my browser.
I’ve had to install add-ons like session manager just to ensure I don’t lose my tabs when working because Firefox’s built-in tools wouldn’t restore my tab session when it crashed.
I recently stumbled upon Firefox Plumber (http://www.rizone3.com/2011/firefox-plumber/). I’ve been testing it for a few days now and I have to say I’m completely blown away by how well the utility works.
Currently I have 32 tabs open in Firefox. Under normal loads, the browser would utilize between 800MB-1GB of memory. With Firefox Plumber running, which is only utilizing about 708k in memory usage, Firefox is currently fluctuating between 4 and 7 MB of memory usage.
The only caveat is that Firefox Plumber does utilize about ~5% of the CPU to keep Firefox tamed.
After some sleuthing, I found it accomplishes this by offloading the memory to the swap file which could present it’s own issues. I’m going to stick with it for now since Firefox appears to be more stable and see how things work.