I had found this code on Smashing Magazine:
window.browser = (function () {
return window.msBrowser ||
window.browser ||
window.chrome;
})();
I had noticed that this expression seemed equal to this one:
browser=window.msBrowser||window.browser||window.chrome;
Side note on the above code
It was supposed to be used in a browser extension, but it doesn't work in background scripts because they don't support
window
. Some other code that should work is:if(typeof msBrowser!=="undefined")var browser=msBrowser; else if(typeof chrome!=="undefined")var browser=chrome; else if(typeof browser==="undefined")throw "UnsupportedBrowserError";
Is there a reason that the author had written the code in the way above? Not knowing the answer, I just decided to keep it the way it is. If there is a reason, what is it, and if there isn't a reason, would it be safe to use the one line alternative or should I use something else?