【问题标题】:Error: It isn't possible to write into a document from an asynchronously-loaded external script错误:无法从异步加载的外部脚本写入文档
【发布时间】:2017-03-31 12:06:47
【问题描述】:

我试图在 Rails 4 网站上加载广告并不断收到以下错误

onejs?MarketPlace=US&adInstanceId=xxxxxxxx&storeId=xxxxxxx:1 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.

如果我刷新页面,广告就会正常加载。这是来自亚马逊的广告代码,位于 show.html.erb 文件中。

<script src="//z-na.amazon-adsystem.com/widgets/onejs?MarketPlace=US&adInstanceId=xxxxxxxxx&storeId=xxxxxxxx"></script>

如果我使用带有 iframe 代码的亚马逊广告,则没有问题,但这种格式的广告没有 iframe 选项(这是原生广告 - 扫描页面以获取相关内容以显示广告)

它是一个在 heroku 上启用了 turbolinks 的 rails 4 站点。我完全不知道如何解决它。这也发生在 adwords 和 media.net 广告中。

知道怎么解决吗?

【问题讨论】:

    标签: javascript ruby-on-rails ruby-on-rails-4 heroku amazonads


    【解决方案1】:

    这个库可能会解决你的问题https://github.com/krux/postscribe

    异步编写 javascript,即使使用 document.write。

    <div id="ad"><h5>Advertisement</h5></div>
    
    <script type="text/javascript">
      // jQuery used as an example of delaying until load.
      $(function() {
        // Build url params and make the ad call
        postscribe('#ad', '<script src=doubleclick_url_with_params><\/script>');
      });
    </script>
    

    【讨论】:

    • 不使用requirejs、commonjs或AMD有什么办法吗?
    • 您也可以使用 ajax 请求 js 文件,然后在脚本加载后执行 eval。任何 document.write 都可能需要被劫持。
    猜你喜欢
    • 2021-04-25
    • 2018-12-12
    • 2023-03-30
    • 2015-09-10
    • 2017-05-02
    • 1970-01-01
    • 2013-09-17
    • 2020-05-08
    • 1970-01-01
    相关资源
    最近更新 更多