7

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?

Yesthe Cia
  • 528
  • 1
  • 7
  • 20

1 Answers1

12

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>
joelnet
  • 13,621
  • 5
  • 35
  • 49
  • Is there any way to do it without using requirejs, commonjs or AMD? – Yesthe Cia Nov 22 '16 at 13:12
  • You can also use ajax to request the js file and then do an eval on the script once loaded. Any document.write's might need to be hijacked. – joelnet Nov 23 '16 at 05:11