Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript onload isn't working ("Uncaught TypeError: Cannot set property 'document.body' of null ")

Here is my codes:

Javascript:

document.body.onload = function() {
    function UP() {//Ẩn hiện nút UP
        if(window.pageYOffset > window.innerHeight)
            document.getElementById('UP').style.display = "block";
    }
}

HTML tag:

<button id="UP" onclick="toTop(350);"></button>

CSS style:

#UP {display: none;}

My idea is when I scroll down, then UP button will appear. But don't know why, it won't, it even return the error: "Uncaught TypeError: Cannot set property 'document.body' of null". Of couse, I did change other script like this:

function getReady() {
    UP();
}
function UP() {//Ẩn hiện nút UP
    if(window.pageYOffset > window.innerHeight)
        document.getElementById('UP').style.display = "block";
}

HTML tag:

<body onload="getReady();">
    <button id="UP" onclick="toTop(350);"></button>
</body>

It is even more terrible. When I load site, I didn't see onload attr of body tag and the browser didn't return any error for me.

Some other infomation: I develop on both side, Cr & FF, it all says a same problem. And I'm working on Wordpress source.

like image 614
Star Light Avatar asked Sep 17 '25 16:09

Star Light


1 Answers

Just to clarify @laruiss and explain what is going on, lets start with your code:

/* body.onload is not recommended, window.onload is. But thats not the main issue*/
window.onload = function() {
    /* You are declaring a function here. Very important: 
     * its a declaration, NOT a function call 
     */
    function UP() {
        if(window.pageYOffset > window.innerHeight)
            document.getElementById('UP').style.display = "block";
    }
    /* So if you want to execute ('call') your function, you have two options:
     * - Remove the declaration around your function aka `function UP(){` and the ending `}`
     * - Call the function here. To make your code work instantly, lets do that.
     */
    UP();
}

When using the onload event, you are actually doing the right thing. True, it's not recommended to actually do that in your DOM, but it should work. The biggest issue that DOM load is subjective. Some browsers will consider the body loaded as soon as the content is in, others will wait for images, and older IE versions won't fire it all! So if you use the recommended window.onload, you should be fine, as it will fire as soon as the DOM is ready. There is another great event for this you might want to consider, which is DOMContentLoaded, but its not as widely supported. Theres a bit more (by clicking through as well) here: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onload

I would also like to add that even though you put your button to display, its has no content so it could appear either empty or if you are resetting your button styles, not at all. Something to keep in mind.

like image 91
somethinghere Avatar answered Sep 20 '25 07:09

somethinghere