Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   inline javascript not working while reading from XMLHttpRequest object (http://www.velocityreviews.com/forums/t925217-inline-javascript-not-working-while-reading-from-xmlhttprequest-object.html)

4levels@gmail.com 06-09-2006 01:59 PM

inline javascript not working while reading from XMLHttpRequest object
 
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!


Laurent Bugnion 06-09-2006 02:35 PM

Re: inline javascript not working while reading from XMLHttpRequestobject
 
Hi,

4levels@gmail.com wrote:
> 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
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch


All times are GMT. The time now is 07:23 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.