Can't access function inside dynamically added script source

Discussion in 'Javascript' started by sylver, Jun 20, 2007.

  1. sylver

    sylver Guest

    Hi,

    Following the discussion that can be found here:
    http://groups.google.com/group/comp...y add a script source&rnum=1#909da813c8527282

    I modified mine and was trying to launch an alert message inside the
    dynamically added script [data.js].

    My sample codes
    ================================================
    [data.js]

    var remoteValue = 'Blah blah';
    alert('File loaded... ' + remoteValue);

    function alertME()
    {
    alert('alertME in data.js');
    }


    HTML:
    
    <a href="#" onclick="addScript('data.js');">START</a>
    
    <script type="text/javascript">
    var x = 'im blank';
    alert(x);
    function addButton()
    {
    var oButton = document.createElement('input');
    oButton.value = 'Add script element';
    oButton.setAttribute("onclick", "alertME();");
    oButton.type = 'button';
    document.body.appendChild(oButton);
    }
    
    function getValues(x)
    {
    alert(x);                 // Shows 'removeValue'
    setTimeout('alert('+x+')',0);  // Shows 'Blah blah'
    }
    
    function addScript(uri)
    {
    var oScript = document.createElement('script');
    oScript.src = uri;
    //var head = document.getElementsByTagName('head')[0];
    //head.appendChild(oScript);
    document.body.appendChild(oScript); //-> head or body, both works
    in Firefox 2.0.0.4
    
    addButton();
    }
    </script>
    
    The alert messages for 'x' appeared correctly. But Firebug return this
    error: alertME is not defined, and 'alertME in data.js' never appear.
    
    Is this a limitation in Javascript? Or Did I miss out on something
    important?
    
    Your suggestion are trully appreciated.
    Thanks.
     
    sylver, Jun 20, 2007
    #1
    1. Advertising

  2. sylver

    Pete Guest

    try this instead

    oButton.onclick = function() {alertME();};
     
    Pete, Jun 20, 2007
    #2
    1. Advertising

  3. sylver

    sylver Guest

    On Jun 20, 1:59 pm, Pete <> wrote:
    > try this instead
    >
    > oButton.onclick = function() {alertME();};


    Thanks for your reply.

    I have tried that line of code before and strangely, it didn't work.
    That's why I replaced it with oButton.setAttribute("onclick",
    "alertME();");

    The above code does actually work. I missplaced the correct "data.js"
    inside another folder. After placing it inside the correct folder, it
    works like a charm.

    Thanks for your time.
     
    sylver, Jun 20, 2007
    #3
    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. Bennett Haselton
    Replies:
    1
    Views:
    516
    John M Deal
    Nov 8, 2004
  2. Jeffrey Todd
    Replies:
    1
    Views:
    4,109
    Peter Blum
    Jun 2, 2005
  3. Weijiajun
    Replies:
    0
    Views:
    341
    Weijiajun
    Jun 4, 2005
  4. access dynamically added controls

    , Jun 8, 2007, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    779
    Teemu Keiski
    Jun 13, 2007
  5. Tereska
    Replies:
    2
    Views:
    155
    Randy Webb
    Dec 16, 2006
Loading...

Share This Page