'Browser Detection in ReactJS
Is there any way to detect IE browser with React and either redirect to a page or give any helpful message. I found something in JavaScript, but not sure how would I use it with React+TypeScript.
var isEdge = !isIE && !!window.StyleMedia;
Solution 1:[1]
Not sure why but nobody mentioned this package: react-device-detect The package have a lot browsers checks, plus versions and some other info related. It's really small and it's updated.
You can use:
import { isIE } from 'react-device-detect';
isIE // returns true or false
react-device-detect it's also very small bundlephobia link
Solution 2:[2]
This is the service I always use when doing JS/Browser based browser-detection: http://is.js.org/
if (is.ie() || is.edge()) {
window.location.href = 'http://example.com';
}
Solution 3:[3]
I was using Gatsby for our React site and build was giving me trouble with the accepted answer, so I ended up using a useEffect on load to be able to not render for IE at a minimum:
const [isIE, setIsIE] = React.useState(false);
React.useEffect(() => {
console.log(`UA: ${window.navigator.userAgent}`);
var msie = window.navigator.userAgent.indexOf("MSIE ");
setIsIE(msie > 0)
}, []);
if(isIE) {
return <></>
}
// In my component render
if(isIE) { return <></> }
Got the idea originally from:
https://medium.com/react-review/how-to-create-a-custom-usedevicedetect-react-hook-f5a1bfe64599
and
Solution 4:[4]
Try:
const isEdge = window.navigator.userAgent.indexOf('Edge') != -1
const isIE = window.navigator.userAgent.indexOf('Trident') != -1 && !isEdge
etc.
Each browser has a distinct user agent you can check.
These can be faked by the client of course, but in my opinion, are a more reliable long term solution.
Solution 5:[5]
You can write test for IE like this.
<script>
// Internet Explorer 6-11
const isIE = document.documentMode;
if (isIE){
window.alert(
"Your MESSAGE here."
)
}
</script>
Solution 6:[6]
This almost broke me, but I found something which seems pretty simple and straight forward, use the vendor name. ie. Google, Apple etc. navigator.vendor.includes('Apple')
I hope this helps someone out there.
Solution 7:[7]
You can try this:
navigator.browserDetection= (function(){
var ua= navigator.userAgent, tem,
M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if(/trident/i.test(M[1])){
tem= /\brv[ :]+(\d+)/g.exec(ua) || [];
return 'IE '+(tem[1] || '');
}
if(M[1]=== 'Chrome'){
tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
}
M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
return M.join(' ');
})();
console.log(navigator.browserDetection); // outputs: `Chrome 92`
Solution 8:[8]
This is all information you can get from your the browser of you client (using react):
let latitude
let longitude
const location = window.navigator && window.navigator.geolocation
if (location) {
location.getCurrentPosition(position => {
latitude = position.coords.latitude
longitude = position.coords.longitude
})
}
var info = {
timeOpened: new Date(),
timezone: new Date().getTimezoneOffset() / 60,
pageon: window.location.pathname,
referrer: document.referrer,
previousSites: window.history.length,
browserName: window.navigator.appName,
browserEngine: window.navigator.product,
browserVersion1a: window.navigator.appVersion,
browserVersion1b: navigator.userAgent,
browserLanguage: navigator.language,
browserOnline: navigator.onLine,
browserPlatform: navigator.platform,
javaEnabled: navigator.javaEnabled(),
dataCookiesEnabled: navigator.cookieEnabled,
dataCookies1: document.cookie,
dataCookies2: decodeURIComponent(document.cookie.split(';')),
dataStorage: localStorage,
sizeScreenW: window.screen.width,
sizeScreenH: window.screen.height,
sizeDocW: window.document.width,
sizeDocH: window.document.height,
sizeInW: window.innerWidth,
sizeInH: window.innerHeight,
sizeAvailW: window.screen.availWidth,
sizeAvailH: window.screen.availHeight,
scrColorDepth: window.screen.colorDepth,
scrPixelDepth: window.screen.pixelDepth,
latitude,
longitude
}
console.log(info)
The browser is browserName
Solution 9:[9]
There is a new package that takes care of this for React: https://www.npmjs.com/package/react-browser-navigator
This is how you can use it:
// import the module
import useNavigator from "react-browser-navigator";
function App() {
// importing the property
let { userAgent } = useNavigator();
// you can use it within the useEffect hook OR simply print the
// string into the return statement
useEffect(() => {
if (!isNull(userAgent)) {
// printing out the entire object
console.log("userAgent", userAgent);
}
}, [userAgent]);
return (
<div>
<span>userAgent:</span> {userAgent}
</div>
);
}
Essentially, the output will be something like this:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
