
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript: How to Detect if a Website is Open on a Mobile or a Desktop?
We can use the CSS media queries to check whether the website is opened inside a web browser or a mobile browser. This information can be fetched using the min-width and the max-width of the webpage.
CSS media queries are only limited to styling the web pages but we can control the functionality of a website as per the user’s device by using the navigator properties in JavaScript.
The Navigator returns a set of values that includes user browser, version, operating system, and many more.
Syntax
navigator.userAgent
Example
In the below example, we are going to use Navigator to fetch the user’s device details. This will fetch us the major info that includes user browser, version, operating system, and more.
# index.html
<!DOCTYPE html> <html> <head> <title>Filtering the Non-unique characters</title> </head> <body> <h1 style="color: green;"> Welcome To Tutorials Point </h1> <script> /* Storing user's device details in a variable*/ let details = navigator.userAgent; /* Creating a regular expression containing some mobile devices keywords to search it in details string*/ let regexp = /android|iphone|kindle|ipad/i; /* Using test() method to search regexp in details it returns boolean value*/ let isMobileDevice = regexp.test(details); if (isMobileDevice) { document.write("<h3>Its a Mobile Device !</h3>"); } else { document.write("<h3>Its a Desktop !</h3>"); } </script> </body> </html>
Output
When the webpage is open on the desktop −
When the webpage is open on mobile −
- Related Articles
- How to detect a mobile device with JavaScript?
- How to detect if JavaScript is disabled in a browser?
- How to Detect if CAPS Lock is ON using JavaScript?
- How to make iOS UIWebView display a mobile website correctly?
- How do I open a website in a Tkinter window?
- How to disable right-clicking on a website using JavaScript?
- How to detect if a user is using an Android tablet or a phone using Kotlin?
- How to open a website in Android’s web browser from any application?
- How to quickly check if a file (workbook) is open or closed in Excel
- How to include Bootstrap Plugins on a website
- How to open a webcam using JavaScript?
- How to open a website in iOS's web browser from my application?
- How to detect if a given year is a Leap year in Oracle?
- How to download a website page on Linux terminal?
- JavaScript: How to Check if a String is a Literal or an Object?
