SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Why is window.onload being called?

    Hi All

    I'm an itinerant programmer who winds up using Javascript occassionally; I suspect this is a simple problem caused by my not knowing the ins and outs of Javascript but I can't figure out what is happening and hope you guys can help me out.

    Years back I wrote some code using Mike Foster's CBE library and the code is overdue an overhaul. I'm trying to convert it to Mike's X Library (libraries at cross-browser.com).

    I have a window.onload function that dynamically creates a set of paired div elements, say form1 & summary1, form2 & summary2; it then sets the summary elements to be visible (display="block") and the form ones to be invisible (display="none").

    Then I have a "toggle" function that switches visibility between a summary element and it's matching form element.

    Toggling from summary to form works fine. But when I toggle back from form to summary the window.onload function gets called again and resets the whole HTML page back to it's initial state.

    I hope that's enough detail, I can give more if needed. Basically I don't understand why window.onload is being called when the page is just being rearranged and not loaded, and why it only happens when I hide the form, and not when I hide the summary in order to display the form.

    Any ideas appreciated.

    thanks
    kirsty

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,608
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    window.onload only gets called when the page first finishes loading. Most scripts that use the DOM have a window.onload call (or equivalent) as that is the earliest point in the processing where you know that the whole page is loaded and that the objects in the page that you are going to refer to actually exist.
    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="^$">

  3. #3
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Thanks, I got it sussed in the end.

    The button I was pressing to toggle between the form and the summary was set up with "<button onclick='toggle....." rather than "<input type=button onclick='toggle......"

    The "button" version of the button on the form was submitting the form, and since the form is only a dummy at the moment, it was reloading the page, and hence it looked like onload was getting called again (because it was :-)).

    The "input" version of the button works fine and doesn't try to submit the form.

    thanks
    kirsty


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
  •