0

I'm new in this board but I have read you for years. :)

I'm trying to learn to code, but I'm in the first steps yet. However, I'm going to use a script to detect the user's browser.

This work with all the mainly browser (I tried it on chrome, firefox and IE), but it doesn't work with Edge. How can I fix it? Thank you!

<p style="text-align: center;">Hi user, you're using: 

<script>

var ba = ["Chrome","Firefox","Safari","Opera","MSIE","Trident","Edge"];
var b, ua = navigator.userAgent;
for(var i=0; i < ba.length; i++){
if( ua.indexOf(ba[i]) > -1 ){
    b = ba[i];
    break;
    }
}
if(b == "MSIE" || b == "Trident" || b == "Edge 20+"){
b = "Internet Explorer";
}
document.write(b + " browser");

</script>
Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22
  • The user agent string is not reliable, since it can be spoofed. Check this answer for a better solution https://stackoverflow.com/a/9851769/5064575 – Rick Oct 12 '17 at 07:52

1 Answers1

1

As you are just starting out, most browser have developer tools, which if you look at the network setting you can see the request headers for each item requested. This will generally show you the user agent string, but as highlighted in comments, this can be spoofed / changed quite easily. You can use the browser tools to aid you when writing your client side javascript, etc. Good luck and enjoy.

f12 Developer Tools

Paul Hadfield
  • 6,088
  • 2
  • 35
  • 56