美文网首页
vue、react等单页应用在微信浏览器中修改标题

vue、react等单页应用在微信浏览器中修改标题

作者: 屎香味十足 | 来源:发表于2017-10-24 23:31 被阅读0次

    由于单页应用是通过路由切换展示不同页面的,而不是真正的跳转链接,然后在IOS系统,微信浏览器中直接用修改title的值不会有效果,所以需要使用特殊方式来修改微信标题,一言不合就上代码:

    /**
    * 微信浏览器中设置对应页面的标题
    * 解决:IOS微信浏览器中用document.title 设置标题无效
    * */
    export const setTitle = (title) => {
       var body = document.getElementsByTagName('body')[0];
       document.title = title;
       var iframe = document.createElement("iframe");
       iframe.setAttribute("src", "logo.png");
       iframe.setAttribute("style", "display:none");
       iframe.addEventListener('load', function() {
           setTimeout(function() {
               try{
                   iframe.removeEventListener('load');
               }catch (err){}
               document.body.removeChild(iframe);
           }, 0);
       });
       document.body.appendChild(iframe);
    };
    

    相关文章

      网友评论

          本文标题:vue、react等单页应用在微信浏览器中修改标题

          本文链接:https://www.haomeiwen.com/subject/yedfpxtx.html