Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to check user agent in angular

How to check user agent in angular ?

Aman Jain, October 18, 2022March 16, 2024

In this post, we will show you how to check user agent in Angular. user-agent header is a string that contains information about the browser and operating system. It is set by the browser and sent to the server with every request. It can also be used to determine if the browser is a mobile device or a desktop browser or os version.

In core javascript we use window.navigator properties to get the user agent and in angular we also use the same because there is no alternatives for it.

this example of check user agent and get user agent in angular will work in all angular version including angular 2, angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13 and angular 14 application.

Simplest way to use check user agent in angular

We will be using the navigator object to get the user-agent header.

if (window.navigator.userAgent.indexOf('Firefox') != -1) {
   alert('You are using Firefox.');
}

The above code will check for the Firefox browser. If the browser is Firefox, an alert will be displayed

Similarly, you can check for other browsers like Chrome, Safari, Opera, etc.

You can also check for mobile devices like iPhone, iPad, Android, etc.

if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i)) {
alert('You are using an iPhone or iPod.');
}

Let’s take an example of it

Step 1 : Create an angular app

First step is to setup an angular app using below command

ng new example-app

Step 2: Create a Component

Create a component and add a method checkBrowserAgent

ng g c hello-world

Here we used to focusOut method to handle the input

import { Component, OnInit } from '@angular/core';


@Component({
  selector: 'app-hello-world',
  templateUrl: './hello-world.component.html',
  styleUrls: ['./hello-world.component.css'],
})
export class HelloWorldComponent implements OnInit {
  
 agent = '';
  constructor() {
    this.agent = this.checkBrowserAgent();
  }

  checkBrowserAgent() {
    let userAgent = window.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';
    }
    return browserName;
  }
  ngOnInit() {}
}

Step 3 : Create template and add html

Now, add simple html template with input field

<h2>Check user agent in angular Example</h2>
<h2>Browser name : {{agent}} </h2>

Related

Javascript Angular angularbrowsernamecheckuser-agent

Post navigation

Previous post
Next post

Related Posts

Javascript Arrow function

What is arrow functions in JavaScript / ES6?

June 12, 2021November 8, 2023

arrow functions in JavaScript are introduced in ES6, before es6 we used normal function which needs to write more code to achieve the functionality. let take an example of arrow functions and es5 normal functions are follow: Normal functions of es5 Arrow functions of es6 uhmm! its looking like so…

Read More
Javascript Password and confirm password validation in angular

Password and confirm password validation in angular 14 ?

September 9, 2022March 16, 2024

Angular 14 provides built-in library for validation but it doesn’t have built in validation for password and confirm password. Angular 14 has release and we are excited to work with new feature so i am also updating the most search article with latest version of angular. so in this tutorial…

Read More
Javascript Custom form control in angular 12

Create custom form control in angular 12 / 13

December 19, 2021March 19, 2022

In this article i will show you to use FormControl in child component. By default child component can not access the FormGroup and we can not use FormControl or FormControlName in parent child component approach. You can also read How to make reusable components in angular? for how child and…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version