Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    A way to replace body onload

    Hi.

    I'm trying to port a standalone GuestMap script to XOOPS (it's a PHP CMS). The problem comes when I try to run it into the page. The original script starts a function on body (
    Code:
    <body onload=init()>
    ), but since I'm running into XOOPS template page, I can't have 2 body tags.

    Are there a way I can replace body onload?


    I already tried
    Code:
    <script type="text/javascript">init()</script>
    and
    Code:
    <script type="text/javascript">onload=init()</script>
    but I'm unable to get the page to load properly. Can someone shed me some light?

    Sorry if it's a too obvious question. I know absolutely -NO- JavaScript.

    Thanks in advance,
    -Yuji

  • #2
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your second idea should work unless there is already another onload event for the page and as long as it is parsed after the init function and the rest of the code that the init function uses (which it should be, being onload). If the problem is a conflict with another onload event, here is the 'Swiss Army Knife' solution:
    Code:
    <script type="text/javascript">
    if ( typeof window.addEventListener != "undefined" )
        window.addEventListener( "load", init, false );
    else if ( typeof window.attachEvent != "undefined" ) {
        window.attachEvent( "onload", init );
    }
    else {
        if ( window.onload != null ) {
            var oldOnload = window.onload;
            window.onload = function ( e ) {
                oldOnload( e );
                init();
            };
        }
        else
            window.onload = init;
    }
    </script>

  • #3
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    jscheuer1, thanks a lot for your reply.

    Your solution worked like a charm

    Thanks again! ^^

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need to add an extra conditional block *between* the window.addEventListener and window.attachEvent - for document.addEventListener, because in Opera the load event comes from document, not window.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •