0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【JavaScript】User-AgentでPCとiPadの判別方法

Posted at

私のUser-Agent (MacBook Air)

console.log(navigator.userAgent)
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'

iPadでSafariブラウザを開いた場合のUser-AgentはなぜかMacintoshの名前がある

iPad Chrome
Mozilla/5.0 (iPad; CPU OS 16_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/120.0.6099.119 Mobile/15E148 Safari/604.1

iPad Safari
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.2 Safari/605.1.15

iPadOS13以上から仕様が変わったみたい。
User-Agentに「iPad」の文字列一致だけでは条件が足りないので、「Macintosh」も記述
さらに、('ontouchend' in document)を追加することでiPadを判別できる。

const userAgent = navigator.userAgent.toLowerCase()
if (/ipad|macintosh/.test(userAgent) && ('ontouchend' in document)) {
	console.log('iPadです。')
} else {
	console.log('PCです。')
}

参考

iOS 13以降 iPadのUserAgentに注意 (WKWebView) - Qiita

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?