Skip to content
Advertisement

Failed to execute ‘getComputedStyle’ on ‘Window’: parameter 1 is not of type ‘Element’ I am getting this error

I am new to Javascript and I can’t figure out why am I getting this error. Here is my code:

JavaScript
JavaScript
JavaScript

This error I get in Mozilla: “Uncaught TypeError: Window.getComputedStyle: Argument 1 is not an object.” What could it be?

Advertisement

Answer

Your script tag needs to be moved to the very end of the body. The script loaded to early.

This error I get in Mozilla: “Uncaught TypeError: Window.getComputedStyle: Argument 1 is not an object.” What could it be?

If you read the error message it says it could not find the argument.

The reason for this is that browsers loads JavaScript tags synchronously. It has been a standard since JavaScript was first introduced.

Solution snippet

JavaScript

Adding a console.log you could have seen that the argument for Window.getComputedStyle is indeed not an object

JavaScript

You can also try out the “defer” attribute, however it has been best practice to move the script tags to the end of the body for compatibility with different older browsers. See https://www.w3schools.com/tags/att_script_defer.asp

Another alternative is to use the “DOMContentLoaded” event https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event

JavaScript
Advertisement