1

I have this:

script_url = 'http://externaldomain.com/script.js'
div_id = 'div'+Math.floor(Math.random() * 1000000);
document.write('<div id="'+div_id+'"><scr'+'ipt id="banner-to-load"></scr'+'ipt></div>')

After DOM is ready i do this:

$('#banner-to-load').attr('src',script_url)

Now, script_url appended, but nothing happens. External script has some functions and document.write, but they don't work. How can i run external script? Or if i run it document.write inside will rebuild my already compiled DOM?

2
  • 2
    Or if i run it document.write inside will rebuild my already compiled DOM? Yes. Commented Nov 7, 2011 at 11:57
  • Have you tried $('#banner-to-load').prop('src',script_url);? Commented Nov 7, 2011 at 11:58

3 Answers 3

2

The src attribute is not loaded asynchronously. Load your js with ajax

$.getScript(); or $.ajax({url:source, dataType: "script"});

Have a look here http://api.jquery.com/jQuery.getScript/

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

2 Comments

Yes, that's a common issue.Make a PHP script to use as a cross domain scripting proxy and load it via ajax
Cross domain is no problem. I do it all the time. See my answer.
1

Try setting src before you add it and recheck your script_url.

document.write('<div id="'+div_id+'"><script '
  +'id="banner-to-load" src="' + script_url + '"></scr'+'ipt></div>')

Update: Or make a cross domain Ajax Query. If you control the other page you can allow cross domain queries by setting the "Access-Control-Allow-Origin" header.

1 Comment

You can also use the Yahoo proxy for cross domain queries: developer.yahoo.com/javascript/howto-proxy.html
1

Try to append the banner_to_load to the body object

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.