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 Min Length and Max Length Validation in Angular 12 13

Min Length and Max Length Validation in Angular 12 / 13 ?

March 21, 2022March 21, 2022

Angular provides built-in library for validation, in the same way it gives validation methods to validate the string and numbers length as well. so in this tutorial i am going to explain how to use min length and max length validation in angular. In angular validation library it provides different…

Read More
Javascript Angular ngClass conditional class Example

Angular ngClass conditional class Example

October 8, 2022March 16, 2024

In this blog post, we’ll explore Angular ngClass conditional class Example in component’s styles based on certain conditions. Sometimes in our application we want dynamically change the color, size, font, background images so Angular provides various ways to conditionally apply classes and style to elements. We’ll discuss how to use…

Read More
Softwares disable change detection anagular

How to disable automatic change detection strategy in angular component?

July 4, 2021November 8, 2023

In angular all components by default take the changes as any event occur, but sometime we disable automatic change detection strategy in angular. Thus, to detach the two way binding in component we can use changeDetection strategy in component metadata. Angular Change Detection Strategy is simple to implement using the…

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