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 how to install angular

What is Angular and how to install angular ?

August 29, 2021October 1, 2021

Angular is a framework of javascript. It’s used for creating efficient and single page application with ease. We can define the angular as follow: Angular is a component based component framework. Angular is collection of libraries which include routing, forms, validations, http requests etc. Angular is easy to use and…

Read More

What is pipe in angular and create a custom pipe?

September 2, 2021September 2, 2021

Pipes are used in angular to transform the data or value. Pipes can be able to transform any type of data type like string, date, currency or any data. It can be used both in template as well in component. We can use pipes anywhere in our application and it…

Read More

How to generate component in angular cli?

August 29, 2021August 29, 2021

Angular cli has rich features of generating components, providers, modules and many more. Angular cli can also be used for build production ready product and serve the project for local environment. Usage of Angular cli ng generate command To generate the component ng generate component component_name To generate the modules…

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

  • June 2025
  • 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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version