Search code examples
javascriptruby-on-railsruby-on-rails-4herokuamazon-advertising-api

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


Im trying to load ads on a rails 4 site and keep getting the following error

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.

If I refresh the page the ad loads just fine. Here is the ad code from amazon which is places in a show.html.erb file.

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

If I use the amazon ads with Iframe code there is no issue but this format ad doesn't have an iframe option (It's the Native Ads - scans page for relevant content to display ads)

Its a rails 4 site with turbolinks enabled on heroku. I'm at a total loss as to how to fix it. This is also happening with adwords and media.net ad.

Any idea how to solve?


Solution

  • This library might solve your problem 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>