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.


Detect device by javascript


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");
	var result = "


"+ isMobile.userAgent()+"

Hey!, You are using Mobile

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


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

  • Hanging menu on window scroll

    After scrolling window beyond menu height, a floating bar appears at the top having same menu inside it.

    • 443
    • 13
    • 443
    • facebook icon
    • twitter icon
    • google plus
  • Image selection like checkbox

    Select your image like a checkbox with little jQuery

    • 490
    • 47
    • 490
    • facebook icon
    • twitter icon
    • google plus
  • Real time content edit

    Edit content at client side.

    • 261
    • 1
    • 261
    • facebook icon
    • twitter icon
    • google plus

Latest quries posted

gopa kumarabout 4 years ago

amit singhabout 4 years ago

rahmat aliabout 4 years ago

mandavi tiwariabout 4 years ago

rahul sharmaabout 4 years ago

Ask question
Latest video
Learn Angular
Profile pic
Guest Back