<body onload="something()">

Discussion in 'Javascript' started by tuxedo, Dec 9, 2006.

  1. tuxedo

    tuxedo Guest

    The way the <body onload="something()"> works ensures that the complete html
    document is loaded before something() is executed.

    Can the same be achieved when placing the onload call in document somewhere
    except within the body tag, or must it always be in the body tag?

    For example, if this is placed elsewhere ...

    window.onload(something())

    .... it will run before the full document has loaded, so that does not work.

    Is it possible to refer to the window only when loaded in any other way?
     
    tuxedo, Dec 9, 2006
    #1
    1. Advertisements

  2. tuxedo

    naixn Guest

    tuxedo wrote :
    function dothis(e)
    {
    alert('loaded!');
    }

    1/
    window.onload = dothis;

    2/
    window.addEventListener('load', dothis, false);
    or
    window.attachEvent('load', dothis); // For IE !

    3/
    window.onload = function(e)
    {
    alert('loaded!');
    }

    4/
    window.addEventListener('load', function(e)
    {
    alert('loaded!');
    }, false);
    ir
    window.attacheEvent('load', function(e)
    {
    alert('loaded!');
    }); // for IE !

    Hope this helps you ;)
     
    naixn, Dec 9, 2006
    #2
    1. Advertisements

  3. tuxedo

    tuxedo Guest

    [...]

    Thank you the function runs after the page has loaded, and it certainly
    helps!

    As much as I dislike browser specific code, I understand that I should make
    attachEvent run only on IE, and that I can safely make any other browser
    run addEventListener.
     
    tuxedo, Dec 9, 2006
    #3
  4. Neither of those statements are true. The - attachEvent - method is
    supported (at least on some objects) by browsers other than IE, such as
    Opera versions > 7. The - addEventListener - method can only be used on
    browsers that support it, which is certainly most of the modern ones but
    by no means all. There is also no formal requirement for the window
    object to support - addEventListener - as it is specified as a method of
    Nodes, and the window object is not a node.

    Richard.
     
    Richard Cornford, Dec 9, 2006
    #4
  5. naixn wrote:
    <snip>

    IE will be happier if the first argument is "onload" rather than "load".

    Richard.
     
    Richard Cornford, Dec 9, 2006
    #5
  6. tuxedo

    tuxedo Guest

    Richard Cornford wrote:

    [...]

    Thanks. On second thoughts, I think I'll leave this method out in exchange
    of simply moving the function call below the required loaded elements.
     
    tuxedo, Dec 9, 2006
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.