(Reliably) calling a function from a dynamically loaded script

Discussion in 'Javascript' started by sfeher@gmail.com, Jun 14, 2006.

  1. Guest

    Hi,

    Is there a way to know when a function is available for me to call it
    from a dynamically loaded a javascript?

    I use this code to load the include.js file and then I call
    testIncludeFn() from it:

    <code>

    var html_doc = document.getElementsByTagName('head').item(0);

    var file = document.createElement('script');
    file.setAttribute('language','javascript');
    file.setAttribute('type','text/javascript');
    file.setAttribute('src','/include.js');
    html_doc.appendChild(file);

    // here I would like to be able to wait for the script to become
    available

    testIncludeFn();

    </code>

    It works in IE and sometimes I get it to work in FF 1.5, but I would
    like to have a way of reliably calling testIncludeFn().
    Can I pool somehow the DOM to see if the load completed. How?

    Is there a better approach to it?

    Regards,

    Sebastian
    , Jun 14, 2006
    #1
    1. Advertising

  2. wrote:

    > Is there a way to know when a function is available for me to call it
    > from a dynamically loaded a javascript?


    > Can I pool somehow the DOM to see if the load completed. How?


    Mozilla (and Opera 9 beta) supports e.g.
    var script = document.createElement('script');
    script.addEventListener(
    'load',
    function (evt) {
    // handle load here e.g. call whatever function you want to call
    functionName();
    },
    false
    )
    script.type = 'text/javascript';
    script.src = 'file.js';
    // now append script element into the document tree so that the script
    // file is loaded and executed
    document.getElementsByTagName('head')[0].appendChild(script);

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jun 14, 2006
    #2
    1. Advertising

  3. Guest

    Martin Honnen wrote:
    > var script = document.createElement('script');
    > script.addEventListener(
    > 'load',
    > function (evt) {
    > // handle load here e.g. call whatever function you want to call
    > functionName();
    > },
    > false
    > )


    Thanks Martin, it works great! and it's really simple this way.

    IE6 seems to be a different story though.
    I tried both the equivalent with attachEvent('onload' ... and
    YAHOO.util.Event.addListener( script,'load',callback);
    I'm not getting any error but IE doesn't seem to react to load on the
    script tag.

    Any idea how to get around it?

    Regards,
    Sebastian
    , Jun 14, 2006
    #3
  4. the DtTvB Guest

    wrote:
    > IE6 seems to be a different story though.
    > I tried both the equivalent with attachEvent('onload' ... and
    > YAHOO.util.Event.addListener( script,'load',callback);
    > I'm not getting any error but IE doesn't seem to react to load on the
    > script tag.


    I would use.
    var script = document.createElement('script');
    script.onload = function(e) {
    functionName();
    };
    script.src = '......';

    Still works fine on Mozilla.
    Not sure on Internet Explorer, but in JS reference, it said it was
    available since IE4.
    the DtTvB, Jun 14, 2006
    #4
  5. Guest

    the DtTvB wrote:
    > wrote:
    > > IE6 seems to be a different story though.
    > > I tried both the equivalent with attachEvent('onload' ... and
    > > YAHOO.util.Event.addListener( script,'load',callback);
    > > I'm not getting any error but IE doesn't seem to react to load on the
    > > script tag.

    >
    > I would use.
    > var script = document.createElement('script');
    > script.onload = function(e) {
    > functionName();
    > };
    > script.src = '......';
    >
    > Still works fine on Mozilla.
    > Not sure on Internet Explorer, but in JS reference, it said it was
    > available since IE4.


    I've tried that before and it didn't work for me. One thing that did
    work though was using "onreadystatechange" instead.

    Thanks,
    Sebastian
    , Jun 14, 2006
    #5
  6. Guest

    the DtTvB wrote:
    > wrote:
    > > IE6 seems to be a different story though.
    > > I tried both the equivalent with attachEvent('onload' ... and
    > > YAHOO.util.Event.addListener( script,'load',callback);
    > > I'm not getting any error but IE doesn't seem to react to load on the
    > > script tag.

    >
    > I would use.
    > var script = document.createElement('script');
    > script.onload = function(e) {
    > functionName();
    > };
    > script.src = '......';
    >
    > Still works fine on Mozilla.
    > Not sure on Internet Explorer, but in JS reference, it said it was
    > available since IE4.


    It does work if you declare both:

    script.onload = script.onreadystatechange = function(){
    alert("onLoad"); };

    Regards,
    Sebastian
    , Jun 14, 2006
    #6
    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?QW5kcmV3?=

    How to reliably redirect from login page?

    =?Utf-8?B?QW5kcmV3?=, Jan 10, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    386
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 10, 2006
  2. Replies:
    4
    Views:
    466
  3. Matthew Barnes

    Detecting interactive mode reliably

    Matthew Barnes, Sep 8, 2003, in forum: Python
    Replies:
    0
    Views:
    272
    Matthew Barnes
    Sep 8, 2003
  4. Thea
    Replies:
    7
    Views:
    1,904
  5. Peter Michaux

    Reliably detect when an iframe has loaded?

    Peter Michaux, Jun 16, 2009, in forum: Javascript
    Replies:
    10
    Views:
    1,718
    Michael J. Ryan
    Jun 18, 2009
Loading...

Share This Page