In this tutorial, you will learn how to get the browser version and browser name in the Angular application. Sometimes you have a requirement to get a browser version in angular, or you might also need to get a browser name in angular.
Rest assured, this profound guide will surely help you and might be the ultimate elixir for angular development need.
In this guide, we will reveal every instruction that is going to help you learn the technique to check the angular browser name and angular browser version with simple javascript code.
In the next step, we will install the Angular command-line interface (CLI); it is a tool that makes it easier to bootstrap and develop Angular applications.
Run the command to make the angular development more convenient.
npm install -g @angular/cli
We have assumed you have already followed the previous step and install the angular cli.
After that creating a new application is super easy. Just hit the given command.
ng new ng-demo
After you executed the above command, it has automatically generated a skeleton project beneath the folder ng-demo with a stack of files.
Make sure to get into the app folder.
cd ng-demo
The detectBrowserName() and detectBrowserVersion() functions are being used here to identify the browser name and browser version.
In this step, you have to open and update code in src/app/app.component.ts file.
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
browserName = '';
browserVersion = '';
ngOnInit() {
this.browserName = this.detectBrowserName();
this.browserVersion = this.detectBrowserVersion();
}
detectBrowserName() {
const agent = window.navigator.userAgent.toLowerCase()
switch (true) {
case agent.indexOf('edge') > -1:
return 'edge';
case agent.indexOf('opr') > -1 && !!(<any>window).opr:
return 'opera';
case agent.indexOf('chrome') > -1 && !!(<any>window).chrome:
return 'chrome';
case agent.indexOf('trident') > -1:
return 'ie';
case agent.indexOf('firefox') > -1:
return 'firefox';
case agent.indexOf('safari') > -1:
return 'safari';
default:
return 'other';
}
}
detectBrowserVersion(){
var userAgent = navigator.userAgent, tem,
matchTest = userAgent.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if(/trident/i.test(matchTest[1])){
tem = /\brv[ :]+(\d+)/g.exec(userAgent) || [];
return 'IE '+(tem[1] || '');
}
if(matchTest[1]=== 'Chrome'){
tem = userAgent.match(/\b(OPR|Edge)\/(\d+)/);
if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
}
matchTest= matchTest[2]? [matchTest[1], matchTest[2]]: [navigator.appName, navigator.appVersion, '-?'];
if((tem= userAgent.match(/version\/(\d+)/i))!= null) matchTest.splice(1, 1, tem[1]);
return matchTest.join(' ');
}
}ʼ’
Now that we have written the login for detecting the browser name and browser version, the next step is to use the interpolation to display the browser name and version with the help of the interpolation.
An Interpolation is defined using the double curly braces that permit the user to bind a value to a UI element.
Also, we will use the angular pipe to transform the value in the title case.
In this step, you have to open and update code in src/app/app.component.html file.
<div class="container mt-5">
<h2>Angular Display Browser Name and Version Example</h2>
<table class="table table-striped mt-5">
<thead>
<tr>
<th>Browser Name</th>
<th>Browser Version</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ browserName | titlecase }}</td>
<td>{{ browserVersion | titlecase }}</td>
</tr>
</tbody>
</table>
</div>
Eventually, we must commence the angular development server using the ng command, consequently executing the below command.
ng serve
Open browser and type the given url then hit enter to run the app.
http://localhost:4200
In this angular guide, you have learned how to find out browser names and browser versions in the Angular application. Additionally, we assisted you how to display the browser name and browser version using the angular pipe.
If you want to learn how to get the current route's name, component pathname or…
React show and hide example. In this tutorial, we will show you how to step…
Tabs are one of the best UI elements for displaying multiple contents in a single…
In this tutorial, we will learn how to create a toast notification component in the…
Bootstrap offers tons of custom UI solutions. On top of that, it is easy to…
React js counter using the useReducer hook example. In this post, we will learn how…