I have some HTML structured as follows
<script>
function x()
{
alert('works');
}
</script>
<table>
(...)
</table>
<script>
console.log('autoexec');
</script>
I am loading this HTML from a file in a DIV's innerHTML through an XMLHttpRequest.
Upon completion of the request, this is what I do
div.innerHTML = request.responseText;
var scripts = div.getElementsByTagName("script");
for(var i=0;i<scripts.length;i++) eval(scripts[i].text);
The bottom script, containing code outside a function, gets executed. However the function x() in the top script isn't evaluated and remains unavailable.
What am I missing? Thanks