0%

Hey! Do you have any query related to front end?

Sign in Sign up

Detect device by javascriptBack

It is very easy to detect device by JavaScript through "navigator.userAgent" property. This property returns information about the name, version and platform of the browser. Then JavaScript matches the name of browser, it falls in the category of mobile, it simply alerts that user is using mobile otherwise desktop/laptop.

HTML

Detect device by javascript

Scripts:

var isMobile = {
	Android: function() {return navigator.userAgent.match(/Android/i);},
	BlackBerry: function() {return navigator.userAgent.match(/BlackBerry/i);},
	iOS: function() {return navigator.userAgent.match(/iPhone|iPad|iPod/i);},
	Opera: function() {return navigator.userAgent.match(/Opera Mini/i);},
	Windows: function() {return navigator.userAgent.match(/IEMobile/i);},
	userAgent : function() {return navigator.userAgent},
	any: function() {return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());}
};
var displayResult = document.getElementById("displayResult");
if(isMobile.any()){
	var result = "

"+isMobile.any()+"

"+ isMobile.userAgent()+"

Hey!, You are using Mobile

"; displayResult.innerHTML = result; }else{ var result = "

"+isMobile.userAgent()+"

Hey!, You are using Desktop

"; displayResult.innerHTML = result; }

Although all devices has not been covered but, maximum number of devices has been covered.

how to use

Simply copy and paste the code inside script tag and place it in header tag and, you can place your stuff by replacing alert statement accordingly, if it is for mobile or desktop/laptop.

How to comment:

  • Create an account in discuss by clicking on login next to arrow.
  • Create password for discuss and varify your email id, through your mail.
  • Refresh page and comment.

You might also like

  • How to get system ip through jquery or javascript ?

    This is not possible to access the IP address through javascript.

    • 0
    • 1
    • 0
    • facebook icon
    • twitter icon
    • google plus
  • Centralize your content

    Position your content according to your choice

    • 248
    • 5
    • 248
    • facebook icon
    • twitter icon
    • google plus
  • Crazy nav highlighter plugin

    Style your menu selection

    • 330
    • 6
    • 330
    • facebook icon
    • twitter icon
    • google plus

Latest quries posted

gopa kumarabout 3 years ago

amit singhabout 3 years ago

rahmat aliabout 3 years ago

mandavi tiwariabout 3 years ago

rahul sharmaabout 3 years ago

Ask question
Latest video
Learn Angular
Profile pic
Guest Back
Username
Password