onLoad without BODY tag

Sharky Forums


Results 1 to 4 of 4

Thread: onLoad without BODY tag

  1. #1
    Tiger Shark
    Join Date
    Feb 2001
    Location
    NJ, got a problem with that?
    Posts
    714

    Post onLoad without BODY tag

    I need to call a function upon loading a specific page. Unfortunately, the page is built by a template design, and the body tags are in the header section, the page I need to have this function run is in the main section, so I can't use another <body onLoad="function()"> there. I've read that functions like onLoad, onBlur, etc. can be called implicitly by using window.onload=function, but I can't get that to work. How can I do this? Where would I implicitly call the onload function?

    ------------------
    "My other car is also a Porsche"
    -------------------------
    Abit BH6, rev. 1.01
    PIII-750@931
    256MB PC133 Crucial RAM
    Hercules Prophet 4500
    Diamond Monster MX300 w. Vortex 2 v.2048 drivers
    DirectX 8.0a
    "My other car is also a Porsche"
    -------------------------
    PIV-3.0GHz, 800MHz FSB; 1GB 400MHz SDRAM; Ati Radeon 9800 Pro; SB Audigy 2; Windows XP SP1
    Old rig: Abit BH6, rev. 1.01; PIII-750@931; 256MB PC133 Crucial RAM; Hercules Prophet 4500; Diamond Monster MX300; DirectX 8.0a; 60 GB IBM Deskstar 60XP

  2. #2
    Ursus Arctos Moderatis Grizzly's Avatar
    Join Date
    Sep 2000
    Location
    Providence, RI USA
    Posts
    3,077

    Post

    You can call any JavaScript on page load if you use a <Script> tag in the <body> of your document.

    Typically, I like to place any such scripts right before the </body> tag.

    Say you want to call a function called "ShowMeTheMoney()" on page load....than you can simply:

    <script language="JavaScript">
    ShowMeTheMoney();
    </script>

    </body>
    </html>


    Then, when the page loads, it will exec that function. Easy as that.

  3. #3
    Tiger Shark
    Join Date
    Feb 2001
    Location
    NJ, got a problem with that?
    Posts
    714

    Post

    Great, worked like a charm, guess it didn't like me making that call in the top part of the page (for whatever reason). Now what I don't understand is that my form.reset() call doesn't work properly anymore. Before, I would have an onLoad event in the Body tag, which would call a function that had a form.reset() statement, and a couple of other things. I did this so that when people hit the Back button after the form was submitted, the form would be cleared. Now that I'm calling this function implicitly, the form doesn't reset properly anymore. It doesn't clear any checkboxes that were checked before submitting the form. I suppose I could step through the form in the load function, unchecking all the boxes, but that doesn't seem to be the cleanest way.
    Originally posted by Grizzly:
    You can call any JavaScript on page load if you use a <Script> tag in the <body> of your document.

    Typically, I like to place any such scripts right before the </body> tag.

    Say you want to call a function called "ShowMeTheMoney()" on page load....than you can simply:

    <script language="JavaScript">
    ShowMeTheMoney();
    </script>

    </body>
    </html>


    Then, when the page loads, it will exec that function. Easy as that.


    ------------------
    "My other car is also a Porsche"
    -------------------------
    Abit BH6, rev. 1.01
    PIII-750@931
    256MB PC133 Crucial RAM
    Hercules Prophet 4500
    Diamond Monster MX300 w. Vortex 2 v.2048 drivers
    DirectX 8.0a
    "My other car is also a Porsche"
    -------------------------
    PIV-3.0GHz, 800MHz FSB; 1GB 400MHz SDRAM; Ati Radeon 9800 Pro; SB Audigy 2; Windows XP SP1
    Old rig: Abit BH6, rev. 1.01; PIII-750@931; 256MB PC133 Crucial RAM; Hercules Prophet 4500; Diamond Monster MX300; DirectX 8.0a; 60 GB IBM Deskstar 60XP

  4. #4
    Ursus Arctos Moderatis Grizzly's Avatar
    Join Date
    Sep 2000
    Location
    Providence, RI USA
    Posts
    3,077

    Post

    Yeah the reason why I place these "onLoad" style scripts right before the close of the </body> tag, is because when this script executes, it will only operate on what HTML has been rendered before it.

    So if you make a call to do a form.reset() before the </form> tag closes, than it will undoubtedly fail.


    The form needs to be fully rendered, before the browser reaches the JavaScript.

    So as long as you keep this <script> as the last and final thing before the </body> tag, than you should be all set.

    Otherwise, you can, and most likely will run into trouble.

Posting Permissions

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