移动端PC端自动识别跳转

1.移动跳PC端

 function isPad() {
      return window.location.toString().indexOf('pref=padindex') !== -1
    }

    function isMobile() {
      return /AppleWebKit.*Mobile/i.test(navigator.userAgent) ||
        (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))
    }

    if (isPad() || !isMobile()) {
      if (window.location.href.indexOf('?mobile') < 0) {
        window.location.href = '地址'
      }
    }

2.PC端跳移动端

function isPad() {
      return window.location.toString().indexOf('pref=padindex') !== -1
    }
    function isMobile() {
      return /AppleWebKit.*Mobile/i.test(navigator.userAgent) ||
        (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))
    }
    if (!isPad() && isMobile()) {
      if (window.location.href.indexOf('?mobile') < 0) {
        window.location.href = '地址'
      }
    }

判断是否是移动端

function isMobile() {
  if ('ontouchstart' in window) {
    console.log(true)
  }else {
    console.log(false)
  }
}

你可能感兴趣的:(移动端PC端自动识别跳转)