-2

According to this question, by the time the body onload gets called, all of the objects in the document are in the DOM, and all the images and sub-frames have finished loading.

I have some JS that is supposed to fire at body onload but my attempts to getElementByID are returning null which makes me think it's firing earlier than I think it is.

Am I wrong in my interpretation of body.onload or am I doing something else wrong?

Here is my code:

<script type="text/javascript"> 
    window.document.body.onload = doStuff;

    function doStuff() {
        var txtElement = document.getElementById("myTextField");

        if (txtElement != null) {
            alert(txtElement.value);
        }
        else {
            alert("Element not found!"); //This alert is always thrown
        }
    }
</script>
TheIronCheek
  • 1,077
  • 2
  • 20
  • 50
  • Please add the HTML as well so we can reproduce your issue. – Scott Marcus Aug 07 '17 at 19:59
  • Per your edit, you're doing something else wrong. but what *something* is can't be determined given the code in the question. We need to know how the target element is being created. – Kevin B Aug 07 '17 at 20:07
  • Your edit invalidates an answer. – Kevin B Aug 07 '17 at 20:08
  • @KevinB - I guess I'm not 100% sure. I'm using a form building module for DNN that is generating the form fields for me. I'm not sure when or how the plugin does it but I'm beginning to think my issue is there. – TheIronCheek Aug 07 '17 at 20:13
  • Sounds like it very well could be. anything that adds elements after the fact isn't going to be ready before the onload handler occurs. you'd have to hook into whatever is creating said elements. It sounds like you should probably be using DNN to do whatever it is you're trying to do rather than custom js. – Kevin B Aug 07 '17 at 20:14

1 Answers1

0

You are actually calling the doStuff function before the DOM is loaded because you have parenthesis after the function name, which causes the function to be invoked as soon as that line is encountered.

When you register a function as a callback to an event, you just want to reference the function, not invoke it. Also, you only need to register the "page" load function to the window.

Change:

window.document.body.onload = doStuff();

to:

window.onload = doStuff;

Additionally, you should modernize your code and, instead of using an event property of an element (which only allows for one function to be stored as a callback to an event), you should use the addEventListener() method to register callback functions.

Lastly, the type attribute is no longer necessary on script tags.

<script> 
    window.addEventListener("load", doStuff);

    function doStuff() {
        var txtElement = document.getElementById("myTextField");

        if (txtElement != null) {
            alert(txtElement.value);
        }
        else {
            alert("Element not found!"); //This alert is always thrown
        }
    }
</script>
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • That didn't solve my problem. Apparently it's a duplicate question, though, so I'll go check that out. Hopefully the answer is there. – TheIronCheek Aug 07 '17 at 20:03
  • 1
    @TheIronCheek Trust me. This is a problem. You may have others as well, but this IS an issue that would cause exactly what you are indicating is happening. – Scott Marcus Aug 07 '17 at 20:04
  • @TheIronCheek the duplicate just suggests the same solution as this answer. If this doesn't solve the problem, then you haven't adequately described it. – Kevin B Aug 07 '17 at 20:05
  • @ScottMarcus - I updated the code per your recommendation. The problem still exists which leads me to think the element isn't being generated until after load. I'm using a form builder plugin for DNN so I have no idea when and how the elements are created. – TheIronCheek Aug 07 '17 at 20:11
  • @TheIronCheek As I initially requested in the comments, you should show us all the relevant code so we can help you. – Scott Marcus Aug 07 '17 at 20:12