Error: It isn't possible to write into a document from an asynchronously-loaded external script

Error: It isn't possible to write into a document from an asynchronously-loaded external script

我正在尝试在 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 选项(它是原生广告 - 扫描页面以获取相关内容以展示广告)

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

知道如何解决吗?

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

Asynchronously write javascript, even with 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>