3

I am using jQuery 1.4 and I am trying to append DIVs that contain javascript files from another domain. I have the following inline code at the end of the body, just after loading jQuery:

<script src="jquery-1.4.3.min.js"></script>
<script src="scripts.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
    var html = "<div><script type='text/javascript' src='http://example.com/foo'></script></div>";
    $("#container").append(html);´
});
</script>

The problem is that the output is:

"; $("#container").append(html); } });

and it is not even displayed in #container but at the bottom of the body where the javascript code is.

EDIT: the javascript file inserts html for ads, so it cannot be loaded into the document head.

2 Answers 2

3

Dynamic script loading isn't possible using the method you describe as the string is loaded as HTML and not parsed. Try this:

var head    = document.getElementsByTagName('head')[0];
var script  = document.createElement('script');
script.type = 'text/javascript';
script.src  = 'http://example.com/foo';
head.appendChild(script);
Sign up to request clarification or add additional context in comments.

7 Comments

The problem is that the javascript insert html for ads in the element which the scripts appear. So it's not possible to place in header.
use jsonp in $.ajax ... but I like described method
$.ajax is limited to the same domain, no?
@Automata try var head = document.getElementsByTagName('container')[0];
$,ajax with type jsonp or get is not restricted (not sure about get) you also probably need to call eval on the fetched script (in the div)
|
0

Script tags with type "text/javascript" are being interpreted by the browser and not displayed. There's no logical reason to put a script tag into a div and hope it will display its content.

If you want scripts loaded from another domain, simply append those to the body not wrapping them in other tags. Once these files are loaded and run they might have code in them that appends something to your div#container element (note: I said might).

You need to first get the concept of DOM and scripting right, then ask again. I'll be glad to help you out.

Regards, aefxx

3 Comments

There is no need to be condescending. The reason why I need to insert the javascript in DIVs is because the javascript files out html for ads.
Sorry, I didn't mean to offend you. But still, you need another thought about the basic concepts. Your question makes - pardon me - no sense to me. Maybe you are talking about a div in an iframe?
Why you don't like the basic concepts? It is very cenvenient - separate 1 big program to manu small modules. So I can develop small part - test, debug, etc. and using append() - add to main page! It is very difficult debug each time big site. It is more easy debug some functional module and after assemble together.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.