1

So the situation I am in involves loading a third party script with this code:

var newScript = document.createElement('script');
newScript.src = 'http://someotherDomain.com/foo.js';
newScript.type = 'text/javascript';

document.body.appendChild(newScript);

foo.js contains the code:

document.write (unescape("%3Cscript src='http://someotherDomain.com/bar.js' type='text/javascript'%3E%3C/script%3E"));

The issue is that the first code snippet will execute successfully and pull in foo.js, but after foo.js is fetched the contents are not executed. I thought that after a browser loads a new script it will parse and then run the containing code. This works if I put in foo.js on initial page load, but not afterwards. Am I missing something?

2
  • 4
    document.write doesn't work after page load... Your first code block shows how to add scripts to the page - if you can, just use that method to load the second script from within the first script. Commented Nov 3, 2011 at 0:37
  • Ahhh that makes sense. I unfortunately do not have the ability to modify 'foo.js' right now. Looks like having it load on page load for me. Thanks! Commented Nov 3, 2011 at 0:40

1 Answer 1

1

Answered by Sime Vidas in the comments:

document.write doesn't work after page load... Your first code block shows how to add scripts to the page - if you can, just use that method to load the second script from within the first script.

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

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.