Newbie Javascript Issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • finco
    New Member
    • Apr 2008
    • 1

    Newbie Javascript Issue

    Hi all - can someone please point me in the right direction. I have pages that launch their javascript components fine if the page is loaded on its own. But when the page is loaded AJAX the javascript does not fire. An example is this simple web page:

    [HTML]<html>
    <head>
    <title></title>
    </head>
    <body>
    <script type="text/javascript">
    alert("Hello World")
    </script>
    </body>
    </html>[/HTML]

    When called on it's own, I get a "hello world" alert.

    When I load this page via AJAX into a div, the alert is not displayed.

    I've written many pages which are dependent upon javascript which work fine until called by AJAX. I'm guessing this is simple but I'm missing a key ingredient here.

    Thanks in advance for your help.
    Last edited by gits; Apr 17 '08, 05:57 PM. Reason: added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Yes, the key ingredient is that the JavaScript won't run like that.

    There's two ways, either use eval (the quick, dirty way);

    or dynamically add the script to the parent page, e.g.
    [code=javascript]var script = document.create Element("script ");
    script.type="te xt/javascript";
    script.appendCh ild(document.cr eateTextNode(th eJScode));
    document.getEle mentsByTagName( "head")[0].appendChild(sc ript);[/code]

    Comment

    Working...