Last active
August 29, 2015 14:06
-
-
Save yanknudtskov/1abb5cebec81b95aefc3 to your computer and use it in GitHub Desktop.
JavaScript to return the current user agent and version as a string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function detectBrowserAndVersion() | |
{ | |
var user_agent = navigator.userAgent, user_agent_version, user_agent_match = user_agent.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; | |
if(/trident/i.test(user_agent_match[1])) | |
{ | |
user_agent_version = /\brv[ :]+(\d+)/g.exec(user_agent) || []; | |
return 'IE '+(user_agent_version[1] || ''); | |
} | |
if(user_agent_match[1] === 'Chrome') | |
{ | |
user_agent_version = user_agent.match(/\bOPR\/(\d+)/) | |
if(user_agent_version !== null) | |
return 'Opera '+user_agent_version[1]; | |
} | |
user_agent_match = user_agent_match[2] ? [user_agent_match[1], user_agent_match[2]]: [navigator.appName, navigator.appVersion, '-?']; | |
if( ( user_agent_version = user_agent.match( /version\/(\d+)/i ) ) !== null ) | |
user_agent_match.splice(1, 1, user_agent_version[1]); | |
return user_agent_match.join(' ').toLowerCase(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment