onLoad question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • judy

    onLoad question

    Hi all,
    I need to run two functions from the onLoad event. I prefer not
    combine them into a single funciton.

    Any suggestions on the onLoad syntax?
  • Lee

    #2
    Re: onLoad question

    judy said:[color=blue]
    >
    >Hi all,
    >I need to run two functions from the onLoad event. I prefer not
    >combine them into a single funciton.
    >
    >Any suggestions on the onLoad syntax?[/color]

    The onLoad handler is always a single function, whose body is
    the string that you provide as the HTML ONLOAD attribute value.
    As in any other function, the body can contain more than one
    function call on a single line, separated by semi-colons:

    <body onload="dothis( );dothat()">

    Comment

    • Frances Del Rio

      #3
      Re: onLoad question

      judy wrote:[color=blue]
      > Hi all,
      > I need to run two functions from the onLoad event. I prefer not
      > combine them into a single funciton.
      >
      > Any suggestions on the onLoad syntax?[/color]


      <body onLoad="functio n1();function2( )">

      these functions will be executed when page has finished loading..

      HTH.... Frances

      Comment

      • Rob B

        #4
        Re: onLoad question

        Guessing at your intent...

        <?xml version="1.0" encoding="iso-8859-1"?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
        <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
        <head>
        <title>untitled </title>
        <script type="text/javascript">
        //<![CDATA[

        function add_handler(obj , evt, handler, captures)
        {
        if (obj.addEventLi stener)
        obj.addEventLis tener(evt, handler, captures);
        else if (obj.attachEven t)
        obj.attachEvent ('on' + evt, handler);
        else
        {
        var old_handler = obj['on' + evt];
        if (null == old_handler)
        obj['on' + evt] = handler;
        else obj['on' + evt] = function()
        {
        old_handler();
        handler();
        }
        }
        }

        //]]>
        </script>
        </head>
        <body>
        <script type="text/javascript">
        //<![CDATA[

        function init()
        {
        alert('function init called');
        }

        add_handler(win dow, 'load', init, false);

        //]]>
        </script>
        <script type="text/javascript">
        //<![CDATA[

        function init2()
        {
        alert('init2');
        }

        add_handler(win dow, 'load', function(){aler t('anonymous function
        called')}, false);

        //]]>
        </script>
        </body>
        </html>

        R. Cornford has a smashing example of multiple assignment, somewhat over
        my head (short trip).

        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Michael Winter

          #5
          Re: onLoad question

          On 22 Nov 2004 07:05:22 GMT, Rob B <ferndoc9@hotma il.com> wrote:

          [snip]
          [color=blue]
          > else if (obj.attachEven t)
          > obj.attachEvent ('on' + evt, handler);
          > else
          > {
          > var old_handler = obj['on' + evt];
          > if (null == old_handler)
          > obj['on' + evt] = handler;
          > else obj['on' + evt] = function()
          > {
          > old_handler();
          > handler();
          > }
          > }[/color]

          Whilst simple and functional, it does have a problem. The three methods
          (two, above) produce a varying, and sometimes deficient, environment for
          the listeners.

          A conforming implementation of addEventListene r will result in two things:

          1) Each listener will be passed an event object as its only argument.
          2) The this operator will refer to the current target of the event.

          This, I think you'll agree, is a nice situation as it replicates what
          you'll find if you add your listeners directly with HTML.

          IE's attachEvent does neither, and the only way to make it is to add a
          function on every call which, itself, calls the listener providing the
          missing information. It's not a nice solution, so I tend to avoid
          attachEvent altogether.

          Your on<event> solution adds uncertainty. On an initial call where the
          first branch is taken, listeners will experience something close to what
          they would find with addEventListene r (except with IE, where the event
          object would still be global). However on subsequent calls, this no longer
          happens and the situation becomes much like it would with attachEvent.

          The quickest fix is:

          var old_handler = obj['on' + evt];
          if (null == old_handler)
          obj['on' + evt] = handler;
          else obj['on' + evt] = function(e)
          {
          old_handler.cal l(this, e);
          handler.call(th is, e);
          };

          It doesn't guarantee that the listener will be passed an event object, but
          at least the this operator will be reliable.

          Another improvement would be to perform the concatenation of the 'on'
          prefix only once:

          if(obj.addEvent Listener) {
          /* ... */
          } else {
          evt = 'on' + evt;
          /* ... */
          }

          as string operations are relatively expensive.

          [snip]
          [color=blue]
          > R. Cornford has a smashing example of multiple assignment, somewhat over
          > my head (short trip).[/color]

          Mind pointing to it (message id or subject)?

          Mike

          --
          Michael Winter
          Replace ".invalid" with ".uk" to reply by e-mail.

          Comment

          • Rob B

            #6
            Re: onLoad question







            *** Sent via Developersdex http://www.developersdex.com ***
            Don't just participate in USENET...get rewarded for it!

            Comment

            Working...