SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    taking initial page load stuff out of <body>?

    I'm trying to modify a website that was created without headers to add them in. Currently the pages have a bunch of javascript in the <body> tag. Is it possible to place these calls into a separate javascript call, or do they need to be in the <body> tag? I'm a javascript noob so I really don't know how it works too well.

    For example here's a body tag on one of my pages:

    Code:
    <body onLoad="liveSearchInit(); document['startpointform']['street'].focus(); clearAllCheckboxes(); loadMap();">
    Can I place this into a separate section as such? I don't know the syntax so I'm just guessing:
    Code:
    <script type="text/javascript">
    
    window.onload(liveSearchInit(), document['startpointform']['street'].focus(), clearAllCheckboxes(),loadMap());
    </script>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    It would be better to place them in a function instead.

    Code javascript:
    window.onload = function () {
        liveSearchInit();
        document['startpointform']['street'].focus();
        clearAllCheckboxes();
        loadMap();
    }
    Last edited by paul_wilkins; Mar 6, 2008 at 13:40.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    There is no such thing as body.onload - it is window.onload

    The only reason that it can be placed in the body tag is that the page doesn't have a proper window tag to attach it to.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    window.onload is bad behavior. You should much rather use a DOMready script equivilant like using the Prototype javascript library and doing...

    Code:
    Event.observe(window, 'load', {
        liveSearchInit();
        document['startpointform']['street'].focus();
        clearAllCheckboxes();
        loadMap();
    });

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Cheers Felgall, some mornings one's brain is on autopilot
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    For those who don't want to use libraries, the standard way is

    Code javascript:
    if (window.addEventListener) {
    	window.addEventListener('load', fnName, false);
    } else if (window.attachEvent) {
    	window.attachEvent('onload', fnName);
    }

    And if you want to retain a reference to the this keyword, and take care of memory leaks, there are more advanced ones like the rock solid AddEvent function from Dustin Diaz
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    PHP Otaku Gibb's Avatar
    Join Date
    Jul 2004
    Location
    Texas
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the responses!

    I'll try some of these suggestions and see if I can get it working.


Bookmarks

Posting Permissions

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