inline javascript not working while reading from XMLHttpRequest object

Discussion in 'Javascript' started by 4levels@gmail.com, Jun 9, 2006.

  1. Guest

    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!
    , Jun 9, 2006
    #1
    1. Advertising

  2. Re: inline javascript not working while reading from XMLHttpRequestobject

    Hi,

    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
    Laurent Bugnion, Jun 9, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QnJhdm8=?=

    XMLHTTPRequest with Xpath is not working

    =?Utf-8?B?QnJhdm8=?=, Apr 1, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    531
    =?Utf-8?B?QnJhdm8=?=
    Apr 1, 2004
  2. Abhi
    Replies:
    2
    Views:
    736
    E. Robert Tisdale
    Jul 3, 2003
  3. Alvin
    Replies:
    7
    Views:
    474
    E. Robert Tisdale
    May 6, 2005
  4. Nish
    Replies:
    4
    Views:
    489
    Thomas Stegen
    Oct 8, 2004
  5. Jon Slaughter

    to inline or not to inline

    Jon Slaughter, Apr 20, 2007, in forum: HTML
    Replies:
    9
    Views:
    350
    Andy Dingley
    Apr 20, 2007
Loading...

Share This Page