LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » How To Detect Browser Using Pure Vanilla JavaScript?

How To Detect Browser Using Pure Vanilla JavaScript?

How-To-Detect-Browser-Using-Pure-Vanilla-JavaScript
How to Detect Browser in JavaScript. To detect user browser, YOu need to use only HTML CSS & JavaScript. In this small project (Detect Browser in JavaScript), as you can see in the preview image, there is a ‘Browser’ text with different browser logos Google Chrome, Mozilla Firefox, Microsoft Edge, etc.

Now you can see that all logos have their full opacity, but when you open this HTML page on any of the given browsers, all logos will fade out except one browser logo you’re currently using. If you are feeling difficulties, feel free to contact us.

There are many code snippets available online or on many other blogs and websites but everyone is not able to optimize your blog or website so you need some optimized code snippet. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready to use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure Vanilla JavaScript.
  3. Cross Browser.
  4. No External Files.
  5. Fully Customizable.
  6. Set Opacity On After Detecting.
  7. Top 5 Browser Added.
Recommended For You:
How To Add External CSS File/Code In Blogger/Website?

How To Detect Browser Using Pure Vanilla JavaScript?

There are a few easy and understandable steps to achieve your desired functionality using pure Vanilla JavaScript that we are gonna share below. Follow each step perfectly.

CSS:

<style>
/* Import Google font - Poppins */
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
body{
height: 100vh;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
background: linear-gradient(#252930 50%, #675AFE 50%);
}
::selection{
color: #fff;
background: #675AFE;
}
.wrapper{
display: flex;
flex-wrap: wrap;
background: #fff;
padding: 30px 40px;
align-items: center;
border-radius: 10px;
justify-content: center;
box-shadow: 0 0 20px 0 rgba(0,0,0,0.1);
}
.wrapper h2{
color: #675AFE;
font-size: 46px;
}
.wrapper .logos{
margin-left: 15px;
}
.logos img{
opacity: 0.3;
margin: 0 7px;
transition: opacity 0.4s ease;
}
.logos img:last-child{
margin-right: 0px;
}
</style>

HTML:

<div class="logos">
<img class="chrome" src="logos/chrome.png" alt="chrome" title="Google Chrome">
<img class="firefox" src="logos/firefox.png" alt="firefox" title="Mozilla Firefox">
<img class="edge" src="logos/edge.png" alt="edge" title="Microsoft Edge">
<img class="opera" src="logos/opera.png" alt="opera" title="Opera">
<img class="safari" src="logos/safari.png" alt="safari" title="Apple Safari">
</div>

JavaScript:

<script>
let userAgent = navigator.userAgent;
let browser;
if(userAgent.match(/edg/i)){
browser = "edge";
}else if(userAgent.match(/firefox|fxios/i)){
browser = "firefox";
}else if(userAgent.match(/opr\//i)){
browser = "opera";
}else if(userAgent.match(/chrome|chromium|crios/i)){
browser = "chrome";
}else if(userAgent.match(/safari/i)){
browser = "safari";
}else{
alert("Other browser");
}
const logo = document.querySelector(`.logos .${browser}`);
if(logo){
logo.style.opacity = "1";
}
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Troubleshooting the Errors:

Do it with concentration and patience. Check your alls steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us or If you have any doubts and problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave your precious feedback in our comment form below. Happy development, See you in the next article.

Recommended For You:
Pretty Print JSON Data In HTML In Color Code Using JavaScript

You Like It, Please Share This Recipe With Your Friends Using...

  

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *