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 Insert style tag dynamically in html in angular

How to Insert Style Tag Dynamically in Angular ?

August 4, 2022March 16, 2024

Sometimes we want to insert style tag in angular app, For an instance we have third party APIs or widget and we wanted to add it in our page then in this condition provided script and style are not angular friendly or we can say it can be pure JavaScript…

Read More
Javascript How to Insert script tag dynamically in html in angular

How to Insert script tag dynamically in angular ?

March 24, 2022August 4, 2022

Sometimes we want to insert script tag in angular app, For an instance we have third party APIs or widget and we wanted to add it in our page then in this condition provided script are not angular friendly or we can say it can be pure JavaScript. so to…

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

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

  • August 2025
  • July 2025
  • 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

  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version