Skip to content

在浏览器中如何判断终端设备

判断硬件设备

js
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
    // iPhone
} else if (/(Android)/i.test(navigator.userAgent)) {
    // Android
} else {
    // Window
};

判断浏览器内核

js
// 判断是否IE内核
if(browser.versions.trident){ alert("is IE"); }
// 判断是否webKit内核
if(browser.versions.webKit){ alert("is webKit"); }
// 判断是否移动端
if(browser.versions.mobile||browser.versions.android||browser.versions.ios){ alert("移动端"); }
// 检测浏览器语言
currentLang = navigator.language;   //判断除IE外其他浏览器使用语言
if(!currentLang){//判断IE浏览器使用语言
    currentLang = navigator.browserLanguage;
}
ts
export type URLParam = {
  [key: string]: string|string[],
}

/**
 * 设置页面标题
 * @param title
 */
export function setTitle(title: string) {
  document.title = title;
}

/**
 * 获取URL链接地址中的参数
 * @returns URLParam
 */
export function getURLQuery() {
  const search = window.location.search.substring(1);
  const param: URLParam = {};
  if (search) {
    search.split('&').forEach((pstr) => {
      let key = '';
      let val: string|string[] = '';
      [key, val] = pstr.split('=');
      const item = param[key];
      if (item && item instanceof Array) {
        item.push(val);
      } else if (item as string) {
        param[key] = [item, val] as string[];
      } else {
        param[key] = val;
      }
    });
  }
  return param;
}

/**
 * 是否为wx浏览器环境
 * @returns boolean
 */
export const isWx = () => /micromessenger/i.test(navigator.userAgent);

/**
 * 是否为企业微信浏览器环境
 * @returns boolean
 */
export const isQw = () => /wxwork/i.test(navigator.userAgent);

/**
 * 是否为移动端
 * @returns boolean
 */
export const isMobile = () => /(iPhone|iPod|iPad|Android|ios|SymbianOS|Mobile)/i.test(navigator.userAgent);

/**
 * 是否为windows环境
 * @returns boolean
 */
export const isWindows = () => /windows|win32|win64/i.test(navigator.userAgent);

export const isIOS = () => /(iPhone|iPod|iPad|ios)/i.test(navigator.userAgent);

/**
 * 是否为iframe嵌套
 * @returns boolean
 */
export const isIframe = () => window.self !== window.top;


export const showVConsole = () => {
  if (window) {
    return window.location.search.indexOf('debugger=yes') > -1;
  }
  return false;
};