Detect Browser in JavaScript with Platform.js

In this article, I am going to tell you one of the easiest ways of detecting the browser in JavaScript. To do this task, we are going to use the Platform.js JavaScript library.

Using the Platform.js library, it will become very easy for us to detect the browser just by writing one line of JavaScript code. You can get this library on GutHub. All the complicated code is already written in this library and we just have to use it.

After downloading this library, all we need to do is just find the JavaScript file and include this JavaScript library using the HTML <script> tag before we use it:

<script src="platform.js"></script>

Or,

We can also include it simply from the CDN without downloading it from GitHub. Below is how we can do it:

<script src="https://cdnjs.cloudflare.com/ajax/libs/platform/1.3.5/platform.min.js"></script>

Now it is the time to write our JavaScript code:

console.log(platform.name);

If we run the code and check the browser console, we will find the name of the browser. For example, if we run it on Chrome, we will see “Chrome” and if we run on Opera, we will see “Opera” on the console.

Also, read: JavaScript code for closing the current tab in the browser window

To confirm if our JavaScript code can detect the browser perfectly, test it with different types of browsers. In my case, I have tested it with Chrome, Opera, Firefox and Internet Explorer. Luckily it works for all these browsers and after that, I think to share it with you.

Thanks to the developer of the Platform.js who make it easier for programmers.

Leave a Reply

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