# JS 判断网页是移动端还是桌面端

# 1. 介绍

JS 判断网页是移动端还是PC端,通常使用 navigator.userAgent 判断

使用 current-device (opens new window) 会更方便

注意:

  • 如果在 PC 端 Chrome 开启移动端调试,navigator.userAgent 不能判断为 PC 端

# 2. 使用

安装:

npm install current-device

引入:

// using es modules
import device from "current-device";

// common.js
const device = require("current-device").default;

# 3. 参考

本章目录