美文网首页
微信中修改title的坑

微信中修改title的坑

作者: ismyshellyiqi | 来源:发表于2018-05-10 16:00 被阅读0次
微信中改变title的坑

由于在个页面中会动态的改变的title的名字

//dom.js
export function setDocumentTitle(title = 'index') {
  if (document.title === title) {
    return
  }
  document.title = title
}

但是在微信中 ios的手机不能够改变title,采用下面的方案来hack

export function setDocumentTitle(title = 'index') {
  if (window.document.title === title) {
    return
  }
  document.title = title
  var mobile = navigator.userAgent.toLowerCase()
  if (/iphone|ipad|ipod/.test(mobile)) {
    var iframe = document.createElement('iframe')
    iframe.style.display = 'none'
    // 替换成站标favicon路径或者任意存在的较小的图片即可
    iframe.setAttribute('src', '/favicon.ico')
    var iframeCallback = function () {
      setTimeout(function () {
        iframe.removeEventListener('load', iframeCallback)
        document.body.removeChild(iframe)
      }, 0)
    }
    iframe.addEventListener('load', iframeCallback)
    document.body.appendChild(iframe)
  }
}

参考了deboyblogvue-wechat-title封装的方法

相关文章

网友评论

      本文标题:微信中修改title的坑

      本文链接:https://www.haomeiwen.com/subject/gbttdftx.html