zoukankan      html  css  js  c++  java
  • Failed to execute 'write' on 'Document'动态载入的js不能执行write

    统计代码一般都是直接一个标签,插入js,标签放在哪里,统计图表就放在哪里!

    我现在是稍微改了一下,我自己加了一点js,在页面所有元素都加载完成之后我再动态的把统计js插入到我需要的地方。

    统计代码的script是插入成功了,也加载成功了

    现在问题来了………

    看chrome的提示:

    Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.

    这怎么回事,何解? 

    如果你看到这段文字,说明您正使用RSS阅读或转自《一棵树-博客园》,原文地址:https://www.cnblogs.com/atree/p/document-write-asynchronously-loaded.html 

     解决方法:

    document已经加载解析完毕,文档流已经关闭了

    所以你异步加载的js不可以再往document里写东西了,比如使用document.write

    不过你可以用dom方法添加

    .appendChild()

    .insertBefore()

    .innerHTML

    1、你可以把script加载放在body标签结束之前

    例如:百度统计的统计代码

    <script>
    var _hmt = _hmt || [];
    (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?12356";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
    })();
    </script>

    2、如果你使用jquery,也可以这样:

    $.getScript('[js containing the initialize function]',function(){
        $.getScript('https://maps.googleapis.com/maps/api/js?v=3.exp&callback=initialize');
    }); 
  • 相关阅读:
    Hadoop作业提交多种方案具体流程详解
    教你一步搭建Flume分布式日志系统
    Hadoop详细配置
    linux 运行springboot sqoop项目
    SQL+ 正则表达式
    SQL+ 正则表达式
    DB2中实现正则表达式
    DB2中实现正则表达式
    spring4整合cxf3
    spring4整合cxf3
  • 原文地址:https://www.cnblogs.com/atree/p/document-write-asynchronously-loaded.html
Copyright © 2011-2022 走看看