10

I'm trying to load an external JavaScript file dynamically into an HTML element to preview an ad tag. The script loads and executes but the script contains "document.write" which has an issue executing properly but there are no errors.

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
        <script type="text/javascript">
        $(function() {
            source = 'http://ib.adnxs.com/ttj?id=555281';

            // DOM Insert Approach
            // -----------------------------------
            var script = document.createElement('script');
                script.setAttribute('type', 'text/javascript');
                script.setAttribute('src', source);

            document.body.appendChild(script);
        });
        </script>
    </head>

<body>
</body>
</html>

I can get it to work if

  1. If i move the the source to the same domain for testing
  2. If the script was modified to use document.createElement and appendChild instead of document.write like the code above.

I don't have the ability to modify the script since it being generated and hosted by a 3rd party.

Does anyone know why the document.write will not work correctly? And is there a way to get around this?

Thanks for the help!

4
  • Why aren’t you just putting the script tag inside the element where you want the ad to appear? Most likely, this is how the ad script is designed. Commented Nov 4, 2013 at 16:05
  • Because I'll be receiving the the URL of the script source through an ajax call. This cannot be rendered from a backend script Commented Nov 4, 2013 at 16:07
  • so... how do you expect the ad script to know where to place the ad? Commented Nov 4, 2013 at 16:08
  • the ad script is doing a document.write were ever the script is being executed. so trying to place it inside a div on the page so it will document.write it's code inside the div tag. of course this is the nature of the problem since i cannot find code to do this Commented Nov 4, 2013 at 16:51

4 Answers 4

9

Another solution is to create an iframe, then load the script inside that iframe when the ajax call is ready:

var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
var doc = iframe.contentWindow.document;

// do this whenever you want (f.ex after ajax is made):
doc.open();
doc.write('<script src="http://ib.adnxs.com/ttj?id=555281">\x3C/script>');
doc.close();

That way, the document.write in the end script will not affect your site, just the iframe. You will need to style the iframe to fit the ad.

Sign up to request clarification or add additional context in comments.

1 Comment

this might be the only answer. since we don't know the size we'll have to grab the content width & height inside the iframes after it's loaded. hacky but will probably work.
1

It would be potentially dangerous to load an external script tag asynchronously if it also contains document.write. So I would suggest either using document.write at your end as well:

document.write('\x3Cscript src="http://ib.adnxs.com/ttj?id=555281">\x3C/script>');

Or just a script tag (duh):

<script src="http://ib.adnxs.com/ttj?id=555281"></script>

3 Comments

IMHO there is no need for the initial \x3c in \x3Cscript only for the closing tag.
@RoyiNamir probably true, I just copy/pasted it from some old snippet... :)
Can't just use a script tag since the url is dynamically loaded via an ajax call from a web service. Document.write in this case
0

You do not want document.write on $(function() ( when dom is ready )

Remove the document .ready wrapper.

And move your to - before </body> . ( or to the container location - where you want the widget to appear.)

so :

<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    </head>

<body>

//bla bla...

<script type="text/javascript">

  (function (){

           var source = 'http://ib.adnxs.com/ttj?id=555281';

            // DOM Insert Approach
            // -----------------------------------
            var script = document.createElement('script');
                script.setAttribute('type', 'text/javascript');
                script.setAttribute('src', source);
                document.body.appendChild(script);
   })();

</script>
</body>
</html>

Important :

enter image description here

4 Comments

@David yes. changed the word. :-) - I meant the same. ( if he want users to see his site - he can not do it / should not do it.).
yes true, however, if the script with document.write is loaded async it will probably also clean up the DOM since the request takes some time to perform. See this demo: jsbin.com/OhUDIJE/2/edit
@David yes agree. ( actually there are better chances for this to happen with defer as opposed to async - cuz defer is certainly when dom is ready... a -too late event).
Tried the above code but still doesn't execute the document.write. Tried in FF & Chrome. Are you seeing the ad display when doing this?
0

I had the same problem and came up with a solution. Mine is a little more complex I think because my layout is generated on the client side, after dom-ready, which means I can't render the script include that way. This added an extra step for me, but it might not be an extra step for you if your layout is generated on the server side.

  1. Generate your URL. in your case, its from an ajax request, in my case, its from user interaction
  2. Reload the page with this URL (maybe POST the URL to the server with a newly created form)

If you're able to render the script include on the server, right where it should be, do that and you'll be done. Otherwise:

  1. Create a div on the server side that is hidden, and has a particular ID. This will load the script pre dom-ready and properly fire off document.write.
  2. Scrape the contents of the div that contains the script, you now have the html in a variable and you can put that anywhere on your page!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.