neo
neo

Reputation: 396

Javascript identify whether the browser platform is desktop or mobile(may be/not from device width-height)

Question is as simple as above.

How to identify whether the browser platform is desktop or mobile with javascript

I was trying facebook ads network and it only works for mobile platforms and not for desktop platform.I was wondering how they get it because when I use toogle device toolbar in chrome it identifies the device as not desktop platform and ad is displayed.Can I know the exact code or something smooth and easy as they use to identify whether its desktop of mobile ?

Upvotes: 1

Views: 353

Answers (1)

Petr Odut
Petr Odut

Reputation: 1787

The information is stored in navigator.userAgent variable. For desktop Chrome is userAgent like:

"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"

for iPhone is like:

"5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1"

Upvotes: 1

Related Questions