微信中修改title

//需要jQuery、zepto版
function weixinTitle(){
    var $body = $('body');
    document.title = 'title';
    // hack在微信等webview中无法修改document.title的情况
    var $iframe = $('<iframe src="/favicon.ico"></iframe>');
    $iframe.on('load',function() {
        setTimeout(function() {
            $iframe.off('load').remove();
        }, 0);
    }).appendTo($body);
}
//需要js原生版本
    function weixinTitle(title){
        var $body = document.body;
        document.title = title;
        // hack在微信等webview中无法修改document.title的情况
        var $iframe = createDom('<iframe src="/favicon.ico"></iframe>');
        $iframe.addEventListener('load', load);
        $body.appendChild($iframe);

        function load(){
            setTimeout(function() {
                $iframe.removeEventListener('load', load);
                $body.removeChild($iframe);
            }, 0);
        }

        function createDom(htmlStr){
            var tmp = document.createElement('div');
            tmp.innerHTML = htmlStr;
            var children = tmp.childNodes;
            for (var i = 0; i < children.length; i++) {
                if (children[i].nodeType ===1 ) {
                    return children[i];
                }
            }
            return;
        }
    }
posted @ 2016-10-17 17:48  Silababy  阅读(1380)  评论(0编辑  收藏  举报