How to know request from mobile or desktop

Introduction

Here in this blog, we will learn how to detect mobile or desktop browser in JavaScript. We will see how to check if the request came from mobile or computer in JavaScript.

How to know request from mobile or desktop

Getting Started

The code example in this blog is applicable to any web application which supports JavaScript code. In the below code example we will see, the best way to detect mobile devices and how can a server-side know whether a client-side is a mobile device or PC.

 var isMobile = /iPhone|iPad|iPod|midp|rv:1.2.3.4|ucweb|windows ce|windows mobile|BlackBerry|IEMobile|Opera Mini|Android/i.test(navigator.userAgent);  

The above code is the key code or main building block to detect device type, it work as server-side device detector and it has capability of request browser detection.

The below code, renders a button which calls JavaScript function 'checkdevide'. The JavaScript code identifies requests coming from mobile or PC, here we are not identifying specific devices for example whether it's iPhone or iPod or which mobile, it only identifies that whether it runs in mobile devices or desktop browsers. But you can identify the specific device by modifying the code.

Example of How to know request from mobile or desktop

 <!DOCTYPE html>  
 <html>  
 <head>  
      <title>Device Borwser Detector</title>  
      <script type="text/javascript">  
      function checkdevide(){  
           var isMobile = /iPhone|iPad|iPod|midp|rv:1.2.3.4|ucweb|windows ce|windows mobile|BlackBerry|IEMobile|Opera Mini|Android/i.test(navigator.userAgent);  
                if (isMobile) {  
                alert("You are using Mobile");  
                } else {  
                     alert("You are using Desktop");  
                }  
           }  
      </script>  
 </head>  
 <body>  
 <h1>Device Borwser Detector</h1>  
 Check Device :<button onclick="checkdevide()" width="50" height="50">Check</button>  
 </body>  
 </html>  

How to know request from mobile or desktop

Copy the above complete code and paste it in notepad, save it as '.html'. Open the file in any browser and click on the button you will get the message whether the runs in mobile browser or desktop browser.

javascript detect device name

How to know request from mobile or desktop

Related Articles

  1. Getting querystring value using Javascript
  2. Add new Row into HTML Table Using JQuery and Javascript
  3. Export HTML Table to Excel Using JavaScript

Thanks

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال