How do I check Windows Phone useragent with javascript?

前端 未结 4 1100
梦谈多话
梦谈多话 2020-12-14 17:05

I can check for iPhone with this code:

(navigator.userAgent.match(/iPhone/i))

But I want to target Windows Phone with this userAgent:

4条回答
  •  醉酒成梦
    2020-12-14 18:09

    Windows Phone certainly seems to be the term you want to match. So just exchange iPhone in your matcher with that term and you're good to go!


    As mentioned in the comments: looking also for iemobile will give you an even broader range of detected microsoft mobiles OSes.

    e.g.:

    if(navigator.userAgent.match(/Windows Phone/i)){
        alert('Is a windows phone!');
    }
    
    if(navigator.userAgent.match(/iemobile/i)){
        alert('Is some mobile IE browser!')
    }
    
    // and probably less common, but still useful:
    if(navigator.userAgent.match(/WPDesktop/i)){
        alert('It is a windows phone in desktop mode!')
    }
    

提交回复
热议问题