I need a Javascript script to block internet explorer or microsoft edge visitors to prevent accessing my website on blogger Edit* Someone help me and answered the question across using user-agent
-
1Parse the user agent. You'd be better of doing this server side, JS will require initial rendering – user3783243 Feb 09 '22 at 19:43
-
1... or dup of https://stackoverflow.com/questions/19999388/check-if-user-is-using-ie or https://stackoverflow.com/questions/31757852/how-can-i-detect-internet-explorer-ie-and-microsoft-edge-using-javascript – user3783243 Feb 09 '22 at 19:44
-
4Why do you want to block specific browsers? If your site requires specific features, you should check for them, not the browser. – Barmar Feb 09 '22 at 19:45
-
2Does this answer your question? [Check if user is using IE](https://stackoverflow.com/questions/19999388/check-if-user-is-using-ie) – Tyler Durden Feb 09 '22 at 19:46
-
Yes thx it help me – يوسف محمد Feb 11 '22 at 15:22
2 Answers
First of all, I don't recommend using that on your website. if there is some functionality that doesn't support this browser you can show a notification or popup telling the user to switch his browser to get a better experiment.
but if you see that is the better solution you can check this code example from here to check the user browser:
let userAgent = navigator.userAgent;
let browserName;
if(userAgent.match(/chrome|chromium|crios/i)){
browserName = "chrome";
}else if(userAgent.match(/firefox|fxios/i)){
browserName = "firefox";
} else if(userAgent.match(/safari/i)){
browserName = "safari";
}else if(userAgent.match(/opr\//i)){
browserName = "opera";
} else if(userAgent.match(/edg/i)){
browserName = "edge";
}else{
browserName="No browser detection";
}
document.querySelector("h1").innerText="You are using "+ browserName +" browser";
And this for redirect to another page:
window.location.replace("http://www.EXAMPLE.com");

- 119
- 1
- 9
-
Hello can you add the old version of internet explorer 6,7,8,9 etc.. – يوسف محمد Feb 09 '22 at 21:55
Every browser and app which access a website has something called a "User Agent". The UA is part of the header that is sent/received with each HTTP call.
Since accessing your website is nothing else than sending a GET-request, you can handle this by checking the user agent in the header and re-route users to a page where you can tell them that they should a different browser. Or you could entirely block the access by not displaying any elements if the user agent is either Edge or IE.
Here is an example on how to get the user agent:
https://www.w3schools.com/jsref/prop_nav_useragent.asp
If you want a better example this could also help:
https://learn.microsoft.com/en-us/microsoft-edge/web-platform/user-agent-guidance
TL;DR: All you need to do is to check the user agent. You can either do this by pattern matching or simpler by searching if the string contains something that's only there in IE/Edge browsers and act accordingly, e.g. by re-routing or not showing/hiding all elements. (I'd avoid the 2nd suggestion, since it's not user friendly)

- 159
- 4