Condensing multiple conditionals into function I feel like I'm repeating a ton of code here and there's got to be a better way to do this but am just completely spacing right now. I basically have an else if clause that, if their conditions are met, repeat the same code, but I'm also repeating the same code in the else statement along with a few other conditions.

What would be the best way to condense this? Could I create one master browser check function?

var searchEngine = document.referrer;

if ($('#userName').length){
      if (/Chrome/.test(searchEngine)){
         browser = "Chrome";
      } else if (/Firefox/.test(searchEngine)){
        browser = "FF";
      } else if (/Safari/.test(searchEngine)){
        browser = "Safari";
      }
} else if (/ip(hone|od|ad)/i.test(searchEngine)){
     if (/Chrome/.test(searchEngine)){
        browser = "Chrome";
     } else if (/Firefox/.test(searchEngine)){
        browser = "FF";
     } else if (/Safari/.test(searchEngine)){
        browser = "Safari";
     } 
} else {
     if (/Chrome/.test(searchEngine)){
        browser = "Chrome";
     } else if (/Firefox/.test(searchEngine)){
        browser = "FF";
     } else if (/Safari/.test(searchEngine)){
        browser = "Safari";
     } else if (/ip(hone|od|ad)/i.test(searchEngine)){
        browser = "iOS";
     } else if (/(?:(compatible;.*)?Trident\/7.0)/ig.test(searchEngine)){
        version = 11;
        browser = "IE";
     } else if (/MSIE (\d+\.\d+);/.test(searchEngine)){
        browser = "IE";
     }
   }
The Chrome, Firefox, and Safari duplicated code exists as the first part of every conditional and those three can be moved outside. Then have all of the other logic in an else. You'll end up with the $('#userName').length and /ip(hone|od|ad)/i.test(searchEngine) being empty. The reason I haven't provided a code example is because only you can decide if that's a problem or not.