0
0

More than 1 year has passed since last update.

jsにてユーザーエージェントを判別する方法。PCでのみ表示などが可能

Posted at

まず初めに

こういう時ってないですか?

「レスポンシブだけど、なぜかブレイクポイントを作ってもipadだけ表示されてまう」
「そもそもipadとかiphone,androidやったら表示したくないねんけど」
「なんなんもう。。。」

上記みたいな時って、僕はありました。
masx-widthでブレイクポイント作ってもうまくいかない時

jsで判定して、あとからstyleを追加しよう

とりあえずコード

<script>
const isIPad = /iPad|Macintosh|iPhone|iPod|Android/i.test(navigator.userAgent)
&& 'ontouchend' in document

var pc_only = document.getElementById("pc_only");
if(isIPad){
  pc_only.style.setProperty("display", "none");
}
</script>

pcのみで表示させたい要素に"pc_only"のIDを付与してあげます。

HTMLが全部読み込まれたあとじゃないとダメなので、そこは注意しましょう。

では、良いPHPライフを!!

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