laosiji888

很多开发过微信的人估计都遇到过这样的问题,ios下微信页面标题更改不了,而安卓却可以直接用:document.title="你的标题"。

下面是解决这个问题的hack:

1.jquery方式

setTimeout(function(){
    //需要jQuery
    var $body = $(\'body\');
    document.title = \'test\';
    // hack在微信等webview中无法修改document.title的情况
    var $iframe = $(\'<iframe src="/favicon.ico"></iframe>\');
    $iframe.on(\'load\',function() {
        setTimeout(function() {
            $iframe.off(\'load\').remove();
        }, 0);
    }).appendTo($body);
},0);

2.不依赖jquery

//以下代码可以解决以上问题,不依赖jq
setTimeout(function(){
    //利用iframe的onload事件刷新页面
    document.title = \'test\';
    var iframe = document.createElement(\'iframe\');
    iframe.style.visibility = \'hidden\';
    iframe.style.width = \'1px\';
    iframe.style.height = \'1px\';
    iframe.onload = function () {
        setTimeout(function () {
            document.body.removeChild(iframe);
        }, 0);
    };
    document.body.appendChild(iframe);
},0);

分类:

技术点:

相关文章: