怎么修改ios上微信浏览器title标签

问题1:我现在找到的解决办法是

xxx.setDocumentTitle = function(title) {
    document.title = title;
    if (/ip(hone|od|ad)/i.test(navigator.userAgent)) {
        var i = document.createElement('iframe');
        i.src = '/favicon.ico';
        i.style.display = 'none';
        i.onload = function() {
            setTimeout(function(){
                i.remove();
            }, 9)
        }
        document.body.appendChild(i);
    }
}

这种方法我看不懂,求解释

问题2:为什么会出现这种情况?安卓端的微信浏览器可以用dom修改title,同样代码在ios端为啥没效果?

阅读 5.5k
3 个回答

clipboard.png
直接change函数中 document.title = "xxx"赋值,
你找的代码
/ip(hone|od|ad)/i.test(navigator.userAgent) 判断是ios设备(正则判断navigator.userAgent)

至于为什么
创建一个iframe, 去改, 坐等神评.

webview中包括微信的浏览器IOS设备是无法通过document.title来更改标题的

给你分享一段代码吧

核心思想是内嵌一个iframe去发起一个请求来触发IOS设备中更新title

changeTitle: function (title) {
    var isIOS = /iPad|iPhone|iPod/i.test(navigator.userAgent);
    if(isIOS){
        var $body = $('body');
        document.title = title;
        // 这儿的src可以换成你的一个地址,尽量开销小,响应速度快
        var $iframe = $('<iframe src="http://client.map.baidu.com/shoppingmall/images/transparent.gif"></iframe>');
        $iframe.on('load',function() {
            setTimeout(function() {
                $iframe.off('load').remove();
            }, 0);
        }).appendTo($body);  
    } else {
        document.title = title;
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题