简体   繁体   中英

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 ?

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM