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

  • Hanging menu on window scroll

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

    • 299
    • 12
    • 299
    • facebook icon
    • twitter icon
    • google plus
  • Show thumbnail on browse

    Way to show thumbnail of browsed image.

    • 229
    • 5
    • 229
    • facebook icon
    • twitter icon
    • google plus
  • Validate email by javascript regex

    Validate email field by regular expression

    • 208
    • 4
    • 208
    • 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