Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Based off of another SO reply another SO reply by kennebec, I had problems following the code so I rewrote it to give myself a better understanding of what is being accomplished.

I would be very grateful if you could critique it or point out any problems I have caused or could modify to improve it.

Tested with IE8/9/10/11, FF, Chrome, Opera & Safari.

Original JSFiddle link Updated with changes from thriggle JSFiddle link

function getBrowser() {
 var userAgent = navigator.userAgent, 
 matched = userAgent.match(/(chrome|firefox|opera|safari|msie|trident(?=\/))\/?\s*(\d+)/i) || [],
 version,
 browser = { name: '', version: '' };
 
 // IE Shells; I gaurantee that you will see this with Dell branded OEM IE 11 installs.
 if(/trident/i.test(matched[1])) { 
 version = /\brv[ :]+(\d+)/g.exec(userAgent) || []; 
 
 return { name: 'Internet Explorer', version: (version[1] || '') };
 }
 
 // Not Chrome; at first glance this looks like a block for chrome identification, but 
 // its actually Opera.
 if(matched[1] === 'Chrome') {
 version = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
 
 if(version !== null) {
 return { name: version[1].replace('OPR', 'Opera'), version: version[2] };
 }
 }
 
 // The rest; Chrome, Safari, etc
 matched = matched[2] ? [matched[1], matched[2]] : [navigator.appName, navigator.appVersion, '-?'];
 
 if((version = userAgent.match(/version\/(\d+)/i)) !== null) {
 matched.splice(1, 1, version[1]);
 }
 
 browser.name = (matched[0] !== "") ? matched[0] : "Unknown";
 browser.version = (matched[1] !== "") ? matched[1] : "Unknown"; 
 
 return browser;
}

Based off of another SO reply by kennebec, I had problems following the code so I rewrote it to give myself a better understanding of what is being accomplished.

I would be very grateful if you could critique it or point out any problems I have caused or could modify to improve it.

Tested with IE8/9/10/11, FF, Chrome, Opera & Safari.

Original JSFiddle link Updated with changes from thriggle JSFiddle link

function getBrowser() {
 var userAgent = navigator.userAgent, 
 matched = userAgent.match(/(chrome|firefox|opera|safari|msie|trident(?=\/))\/?\s*(\d+)/i) || [],
 version,
 browser = { name: '', version: '' };
 
 // IE Shells; I gaurantee that you will see this with Dell branded OEM IE 11 installs.
 if(/trident/i.test(matched[1])) { 
 version = /\brv[ :]+(\d+)/g.exec(userAgent) || []; 
 
 return { name: 'Internet Explorer', version: (version[1] || '') };
 }
 
 // Not Chrome; at first glance this looks like a block for chrome identification, but 
 // its actually Opera.
 if(matched[1] === 'Chrome') {
 version = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
 
 if(version !== null) {
 return { name: version[1].replace('OPR', 'Opera'), version: version[2] };
 }
 }
 
 // The rest; Chrome, Safari, etc
 matched = matched[2] ? [matched[1], matched[2]] : [navigator.appName, navigator.appVersion, '-?'];
 
 if((version = userAgent.match(/version\/(\d+)/i)) !== null) {
 matched.splice(1, 1, version[1]);
 }
 
 browser.name = (matched[0] !== "") ? matched[0] : "Unknown";
 browser.version = (matched[1] !== "") ? matched[1] : "Unknown"; 
 
 return browser;
}

Based off of another SO reply by kennebec, I had problems following the code so I rewrote it to give myself a better understanding of what is being accomplished.

I would be very grateful if you could critique it or point out any problems I have caused or could modify to improve it.

Tested with IE8/9/10/11, FF, Chrome, Opera & Safari.

Original JSFiddle link Updated with changes from thriggle JSFiddle link

function getBrowser() {
 var userAgent = navigator.userAgent, 
 matched = userAgent.match(/(chrome|firefox|opera|safari|msie|trident(?=\/))\/?\s*(\d+)/i) || [],
 version,
 browser = { name: '', version: '' };
 
 // IE Shells; I gaurantee that you will see this with Dell branded OEM IE 11 installs.
 if(/trident/i.test(matched[1])) { 
 version = /\brv[ :]+(\d+)/g.exec(userAgent) || []; 
 
 return { name: 'Internet Explorer', version: (version[1] || '') };
 }
 
 // Not Chrome; at first glance this looks like a block for chrome identification, but 
 // its actually Opera.
 if(matched[1] === 'Chrome') {
 version = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
 
 if(version !== null) {
 return { name: version[1].replace('OPR', 'Opera'), version: version[2] };
 }
 }
 
 // The rest; Chrome, Safari, etc
 matched = matched[2] ? [matched[1], matched[2]] : [navigator.appName, navigator.appVersion, '-?'];
 
 if((version = userAgent.match(/version\/(\d+)/i)) !== null) {
 matched.splice(1, 1, version[1]);
 }
 
 browser.name = (matched[0] !== "") ? matched[0] : "Unknown";
 browser.version = (matched[1] !== "") ? matched[1] : "Unknown"; 
 
 return browser;
}
Added an updated jsfiddle link.
Source Link
Ne Ma
  • 221
  • 1
  • 7

Based off of another SO reply by kennebec, I had problems following the code so I rewrote it to give myself a better understanding of what is being accomplished.

I would be very grateful if you could critique it or point out any problems I have caused or could modify to improve it.

Tested with IE8/9/10/11, FF, Chrome, Opera & Safari.

Original JSFiddle link Updated with changes from thriggle JSFiddle link

function getBrowser() {
 var userAgent = navigator.userAgent, 
 matched = userAgent.match(/(chrome|firefox|opera|safari|msie|trident(?=\/))\/?\s*(\d+)/i) || [],
 version,
 browser = { name: '', version: '' };
 
 // IE Shells; I gaurantee that you will see this with Dell branded OEM IE 11 installs.
 if(/trident/i.test(matched[1])) { 
 version = /\brv[ :]+(\d+)/g.exec(userAgent) || []; 
 
 return { name: 'Internet Explorer', version: (version[1] || '') };
 }
 
 // Not Chrome; at first glance this looks like a block for chrome identification, but 
 // its actually Opera.
 if(matched[1] === 'Chrome') {
 version = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
 
 if(version !== null) {
 return { name: version[1].replace('OPR', 'Opera'), version: version[2] };
 }
 }
 
 // The rest; Chrome, Safari, etc
 matched = matched[2] ? [matched[1], matched[2]] : [navigator.appName, navigator.appVersion, '-?'];
 
 if((version = userAgent.match(/version\/(\d+)/i)) !== null) {
 matched.splice(1, 1, version[1]);
 }
 
 browser.name = (matched[0] !== "") ? matched[0] : "Unknown";
 browser.version = (matched[1] !== "") ? matched[1] : "Unknown"; 
 
 return browser;
}

Based off of another SO reply by kennebec, I had problems following the code so I rewrote it to give myself a better understanding of what is being accomplished.

I would be very grateful if you could critique it or point out any problems I have caused or could modify to improve it.

Tested with IE8/9/10/11, FF, Chrome, Opera & Safari.JSFiddle link

function getBrowser() {
 var userAgent = navigator.userAgent, 
 matched = userAgent.match(/(chrome|firefox|opera|safari|msie|trident(?=\/))\/?\s*(\d+)/i) || [],
 version,
 browser = { name: '', version: '' };
 
 // IE Shells; I gaurantee that you will see this with Dell branded OEM IE 11 installs.
 if(/trident/i.test(matched[1])) { 
 version = /\brv[ :]+(\d+)/g.exec(userAgent) || []; 
 
 return { name: 'Internet Explorer', version: (version[1] || '') };
 }
 
 // Not Chrome; at first glance this looks like a block for chrome identification, but 
 // its actually Opera.
 if(matched[1] === 'Chrome') {
 version = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
 
 if(version !== null) {
 return { name: version[1].replace('OPR', 'Opera'), version: version[2] };
 }
 }
 
 // The rest; Chrome, Safari, etc
 matched = matched[2] ? [matched[1], matched[2]] : [navigator.appName, navigator.appVersion, '-?'];
 
 if((version = userAgent.match(/version\/(\d+)/i)) !== null) {
 matched.splice(1, 1, version[1]);
 }
 
 browser.name = (matched[0] !== "") ? matched[0] : "Unknown";
 browser.version = (matched[1] !== "") ? matched[1] : "Unknown"; 
 
 return browser;
}

Based off of another SO reply by kennebec, I had problems following the code so I rewrote it to give myself a better understanding of what is being accomplished.

I would be very grateful if you could critique it or point out any problems I have caused or could modify to improve it.

Tested with IE8/9/10/11, FF, Chrome, Opera & Safari.

Original JSFiddle link Updated with changes from thriggle JSFiddle link

function getBrowser() {
 var userAgent = navigator.userAgent, 
 matched = userAgent.match(/(chrome|firefox|opera|safari|msie|trident(?=\/))\/?\s*(\d+)/i) || [],
 version,
 browser = { name: '', version: '' };
 
 // IE Shells; I gaurantee that you will see this with Dell branded OEM IE 11 installs.
 if(/trident/i.test(matched[1])) { 
 version = /\brv[ :]+(\d+)/g.exec(userAgent) || []; 
 
 return { name: 'Internet Explorer', version: (version[1] || '') };
 }
 
 // Not Chrome; at first glance this looks like a block for chrome identification, but 
 // its actually Opera.
 if(matched[1] === 'Chrome') {
 version = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
 
 if(version !== null) {
 return { name: version[1].replace('OPR', 'Opera'), version: version[2] };
 }
 }
 
 // The rest; Chrome, Safari, etc
 matched = matched[2] ? [matched[1], matched[2]] : [navigator.appName, navigator.appVersion, '-?'];
 
 if((version = userAgent.match(/version\/(\d+)/i)) !== null) {
 matched.splice(1, 1, version[1]);
 }
 
 browser.name = (matched[0] !== "") ? matched[0] : "Unknown";
 browser.version = (matched[1] !== "") ? matched[1] : "Unknown"; 
 
 return browser;
}
edited body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Browser and version detection js

Based off of another SO reply by kennebec;kennebec, I had problems following the code so I rewrote it to give myself a better understanding of what is being accomplished.

I would be very grateful if you could critique it or point out any problems I have caused or could modify to improve it.

Tested with IE8/9/10/11, FF, Chrome, Opera & Safari. JSFiddle link

function getBrowser() {
 var userAgent = navigator.userAgent, 
 matched = userAgent.match(/(chrome|firefox|opera|safari|msie|trident(?=\/))\/?\s*(\d+)/i) || [],
 version,
 browser = { name: '', version: '' };
 
 // IE Shells; I gaurantee that you will see this with Dell branded OEM IE 11 installs.
 if(/trident/i.test(matched[1])) { 
 version = /\brv[ :]+(\d+)/g.exec(userAgent) || []; 
 
 return { name: 'Internet Explorer', version: (version[1] || '') };
 }
 
 // Not Chrome; at first glance this looks like a block for chrome identification, but 
 // its actually Opera.
 if(matched[1] === 'Chrome') {
 version = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
 
 if(version !== null) {
 return { name: version[1].replace('OPR', 'Opera'), version: version[2] };
 }
 }
 
 // The rest; Chrome, Safari, etc
 matched = matched[2] ? [matched[1], matched[2]] : [navigator.appName, navigator.appVersion, '-?'];
 
 if((version = userAgent.match(/version\/(\d+)/i)) !== null) {
 matched.splice(1, 1, version[1]);
 }
 
 browser.name = (matched[0] !== "") ? matched[0] : "Unknown";
 browser.version = (matched[1] !== "") ? matched[1] : "Unknown"; 
 
 return browser;
}

Browser and version detection js

Based off of another SO reply by kennebec; I had problems following the code so I rewrote it to give myself a better understanding of what is being accomplished.

I would be very grateful if you could critique it or point out any problems I have caused or could modify to improve it.

Tested with IE8/9/10/11, FF, Chrome, Opera & Safari. JSFiddle link

function getBrowser() {
 var userAgent = navigator.userAgent, 
 matched = userAgent.match(/(chrome|firefox|opera|safari|msie|trident(?=\/))\/?\s*(\d+)/i) || [],
 version,
 browser = { name: '', version: '' };
 
 // IE Shells; I gaurantee that you will see this with Dell branded OEM IE 11 installs.
 if(/trident/i.test(matched[1])) { 
 version = /\brv[ :]+(\d+)/g.exec(userAgent) || []; 
 
 return { name: 'Internet Explorer', version: (version[1] || '') };
 }
 
 // Not Chrome; at first glance this looks like a block for chrome identification, but 
 // its actually Opera.
 if(matched[1] === 'Chrome') {
 version = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
 
 if(version !== null) {
 return { name: version[1].replace('OPR', 'Opera'), version: version[2] };
 }
 }
 
 // The rest; Chrome, Safari, etc
 matched = matched[2] ? [matched[1], matched[2]] : [navigator.appName, navigator.appVersion, '-?'];
 
 if((version = userAgent.match(/version\/(\d+)/i)) !== null) {
 matched.splice(1, 1, version[1]);
 }
 
 browser.name = (matched[0] !== "") ? matched[0] : "Unknown";
 browser.version = (matched[1] !== "") ? matched[1] : "Unknown"; 
 
 return browser;
}

Browser and version detection

Based off of another SO reply by kennebec, I had problems following the code so I rewrote it to give myself a better understanding of what is being accomplished.

I would be very grateful if you could critique it or point out any problems I have caused or could modify to improve it.

Tested with IE8/9/10/11, FF, Chrome, Opera & Safari. JSFiddle link

function getBrowser() {
 var userAgent = navigator.userAgent, 
 matched = userAgent.match(/(chrome|firefox|opera|safari|msie|trident(?=\/))\/?\s*(\d+)/i) || [],
 version,
 browser = { name: '', version: '' };
 
 // IE Shells; I gaurantee that you will see this with Dell branded OEM IE 11 installs.
 if(/trident/i.test(matched[1])) { 
 version = /\brv[ :]+(\d+)/g.exec(userAgent) || []; 
 
 return { name: 'Internet Explorer', version: (version[1] || '') };
 }
 
 // Not Chrome; at first glance this looks like a block for chrome identification, but 
 // its actually Opera.
 if(matched[1] === 'Chrome') {
 version = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
 
 if(version !== null) {
 return { name: version[1].replace('OPR', 'Opera'), version: version[2] };
 }
 }
 
 // The rest; Chrome, Safari, etc
 matched = matched[2] ? [matched[1], matched[2]] : [navigator.appName, navigator.appVersion, '-?'];
 
 if((version = userAgent.match(/version\/(\d+)/i)) !== null) {
 matched.splice(1, 1, version[1]);
 }
 
 browser.name = (matched[0] !== "") ? matched[0] : "Unknown";
 browser.version = (matched[1] !== "") ? matched[1] : "Unknown"; 
 
 return browser;
}
Reduced code by removing the last two if statements.
Source Link
Ne Ma
  • 221
  • 1
  • 7
Loading
Source Link
Ne Ma
  • 221
  • 1
  • 7
Loading
default

AltStyle によって変換されたページ (->オリジナル) /