2

I stumbled upon a strange behaviour of the XMLHttpRequest. Maybe I'm just not well informed enough about its possibilities or possible how Javascript pages load, so could someone please confirm my question?

When I put plain Javascript in a file that is read-in through a XMLHttpRequest-object, it's like it is totally ignored. Eg. I have the file ajax_include.html with in it's body the following lines

<script type="text/javascript" language="javascript">
    alert('some alert');
</script>

I also tried it with an external javascript file.

When I directly surf to the file, the alert pops up as expected, but when I use a simple XMLHttpRequest to replace the contents of a div with the contents of this page, the alert is not popping up.

Can someone confirm this strange behaviour? Or is it just normal with the use of an XMLHttpRequest object?

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
tawheed
  • 5,565
  • 9
  • 36
  • 63

1 Answers1

4

This has nothing to do with XMLHttpRequest. <script> elements added to a page with innerHTML won't be executed.

See Executing elements inserted with .innerHTML for work arounds.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335