inserting a body onload event handler from a module?  Top

  • Yo.

    I have a small javascript I want to use in a module I'm building. Although, that script can't run until the page has finished loading or it will generate a script error.

    So, my question:
    Is it possible to manipulate the tag from my module and insert a onload event handler?

    Cheers.
  • You can do this in your theme.
  • Yes.. thats true...
    But is that really a nice way to solve my problem?
    If someone else wants to use my module they have to change the theme aswell... nah.. don't think so.

    as I see it I have three options here
    1. There is some way for me to manipulate the body tag from a module, learn how to do it and smile :)
    2. Find out if there is some way to code JavaScript to just wait until the page has finished loading until it starts, learn how to do that and smile. :)
    3. Drop the script and use something else. and smile. :)
  • Use the $additional_header array which is included in the Header.php before the themeheader() function is called:

    Code

    $additional_header = array("<script language='JavaScript'>
    function functionname() {
    etc...
    } // end function
    onload = function() {  functionname();  }
    </script>\n"
    );

    You might have to inform the user in the ReadMe that if they use a BODY onload event to add your function too:

    Code

    <BODY onload="function1(); functionname()">

    Additionally, you may try to call it in the Body near the bottom of your output:

    Code

    <script language="JavaScript1.2"> functionname(); </script>
    however your function will be called as soon as the code is read into the browser, even if the page isn't completely loaded, hence it has to appear at the bottom. You could try wrapping it in a timeout:
    window.setTimeout('functionname(this)',1000);

    Just some suggestions.
  • Thanks,
    This helps alot...
    I've decided to use a different script as default and include the script and a readme on how to activate the other script.

    Thanks for your help and time :)
  • Just to clarify things...

    the code :

    Code

    $additional_header = array("<script language='JavaS........");


    shouldn't that be :

    Code

    $additional_header[] = "<script language='JavaS........";


    otherwise I would risk removing other additional headers made by other modules/blocks?
  • Quite true. Since the Header file doesn't initialise the array, however, not sure about 0.75, you'd put:

    Code

    global $additional_header;
        if (!is_array($additional_header))
          $additional_header = array();
    $additional_header[] = "<script language='JavaS........";
  • Thanks :)
  • Xanthia has a modulejavascript plugin for this to load the javascript depending on the recent module icon_rolleyes

    An onLoad eventhandler is also supported.

    Frank

    --
    "He is not dangerous, he just wants to play...."
  • well.... true... but then you have to have xanthia.. and I have to convert my module to that.. and .. well... I don't have the time :|
  • The Xanthia function you are referring to doesn't work when called from a Module...
    AND it requires your theme to include special tags...
    That option is STRICTLY a workaround for a Theme Developer... NOT a Module developer...

    --
    That was a hell of a thing...
    -------------------------------------
    Galaxy Quest

This list is based on the users active over the last 60 minutes.