I have a JS script that I am using to append a string to the head
tag, it works, but it doesn't execute what was appended.
HTML
file:
<html>
<head>
...
<script src="test.js"></script>
...
</head>
<body>
...
</body>
</html>
File: test.js
var str =
`
<script ... ></script>
<link...>
other stuff...
`
var html = document.getElementsByTagName('head')[0].innerHTML;
document.getElementsByTagName('head')[0].innerHTML=str+html;
With this, I understand that it would go into recursion if all of head
was executed again just to execute the appended string (unless there is a way to execute only the appended string), but I have a way to deal with this. First, I need it to actually execute.
I have seen other similar questions, but they ask about appending a single script
tag or something similar, I'm looking to add a whole chunk of HTML
and have the browser execute it.
I'm looking for a pure JavaScript solution.
Note that str
contains things like JQuery
and Bootstrap
and therefore the rest of the document heavily depends on this append & execution happening first.