很多開發過微信的人估計都遇到過這樣的問題,ios下微信頁面標題更改不了,而安卓卻可以直接用:document.title="你的標題"。 下麵是解決這個問題的hack: 1.jquery方式 2.不依賴jquery ...
很多開發過微信的人估計都遇到過這樣的問題,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);