單頁應用裏整個頁面只會在第一次徹底刷新,後面只會局部刷新(通常不包括head及裏面的title),因此沒法在服務器端控制title,只能在頁面刷新的時候經過js修改title。常規作法以下,惋惜在iOS微信瀏覽器無效。javascript
問題緣由:css
由於微信瀏覽器首次加載頁面初始化title後,就不再監聽 document.title的change事件。java
解決方案:ios
修改title以後,給頁面加上一個內容爲空的iframe,隨後當即刪除這個iframe,這時候會刷新title。可是若是簡單的這樣設置,通常是會有閃動的,因此能夠設置瀏覽器
方法一:服務器
完整代碼:微信
方法二:微信開發
封裝了一個只在IOS的狀態下處理的方法:app
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); } }