Getting the browser that you are using using Javascript can be important in some cases. Like when reading or setting an element’s property, browsers like IE and Firefox both have different ways to do them. Take for example creating a hyperlink using Javascript. To create the text of the hyperlink, IE uses its proprietary innerText property while Firefox uses textContent (although the specific browser will just ignore the property that it cannot recognize, I feel it’s nice to use browser detection to fully ignore the property even if no error will be thrown). Using the function below can be pretty useful in doing conditional statements on which property to apply to an element. The function is custom made, you can modify it if you wish since my only concern was having to know if the browser was IE or not.
|
1 2 3 4 5 6 |
function getBrowser() { var sBrowser = navigator.userAgent; if (sBrowser.toLowerCase().indexOf('msie') > -1) return 'ie'; else if (sBrowser.toLowerCase().indexOf('firefox') > -1) return 'firefox'; else return 'mozilla'; } |