inline javascript not working while reading from XMLHttpRequest object

4

4levels

Dear Folks,

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

When I put plain javscript 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>
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, although
when I view the selection's source (Thank you, Firefox!), it is there!

When I place an anchor with an onclick-action (eg. alert('onclick')),
it works when I click it.
So my "conclusion" is that it seems like inline javascript commands are
ignored (functions not recognized etc.). All actions assigned to other
events work nice though.

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

Many regards already!
 
L

Laurent Bugnion

Hi,

Dear Folks,

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

When I put plain javscript 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>
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, although
when I view the selection's source (Thank you, Firefox!), it is there!

When I place an anchor with an onclick-action (eg. alert('onclick')),
it works when I click it.
So my "conclusion" is that it seems like inline javascript commands are
ignored (functions not recognized etc.). All actions assigned to other
events work nice though.

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

Many regards already!

Actually, the problem is not with the XmlHttpRequest, but rather with
the way the interpreter works.

When a webpage is loaded, every JavaScript code is interpreted, and
global instructions are executed. When you use DOM functions to add some
JavaScript code to the page, and since the page is already loaded, the
global code won't be executed again (what is the behaviour you expected?
That all global JavaScript code on the page would be executed again? Or
only the snippet you add to the page?

Global code is not a good idea anyway. Wrap your code in a function, and
you events or normal function calls to execute it, that's my advice.

Greetings,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top