JQuery: add div containing javascript files?

I am using jQuery 1.4 and I am trying to add DIVs containing javascript files from another domain. I have the following inline code at the end of the body, right 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 conclusion is:

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

and it’s not even displayed in #container, but in the lower part of the body where the javascript code is located.

EDIT: The javascript file inserts html for advertising, so it cannot be loaded into the document header.

+3
source share
2 answers

Loading a dynamic script is not possible using the method you are describing, since the string is loaded as HTML and not parsed. Try the following:

var head    = document.getElementsByTagName('head')[0];
var script  = document.createElement('script');
script.type = 'text/javascript';
script.src  = 'http://example.com/foo';
head.appendChild(script);
+3

Script text/javascript . script div , .

, , , . , , , - div # container (: , ).

DOM , . .

, aefxx

0

Source: https://habr.com/ru/post/1774731/


All Articles