Javascript does not continue after adding stylesheet to class

I want to display a container div if the device has a special width. I made a simple if condition and it works well.

I wanted to add a second function to my code, and the function was not called. After checking, I saw that the code stops after document.getElementyById-Code.

If a warning ("Hello World") is called in an if condition before getElementById, it is called. If it is called in an if condition after getElementById, it is no longer called.

Why is progress stuck there?

Console output: [Error] TypeError: null is not an object (evaluation is "document.getElementById (" whitecontainer "). Style ') setHeightandWidth (-Link deleted-, line 402) onresize (-Link deleted-, line 382)

but the object cannot be null because changing the style does work, it just gets stuck after changing the size.

Thanks for the help!

function setHeightandWidth() { var body = document.body, html = document.documentElement; var height = (Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight) - 75 - 248); var width = document.body.clientWidth; if (width < 1199) { document.getElementById("whitecontainer").style.display = "none"; alert("Hello World!"); } if (width >= 1199) { width = 1140; document.getElementById("whitecontainer").style.display = "inherit"; } var white_container = document.getElementById("whitecontainer"); white_container.style.height = height + "px"; white_container.style.width = width + "px"; } 
+6
source share
2 answers

The warning does not work, probably means that the line before this caused an error.

Make lines

 white_container.style.height = height + "px"; white_container.style.width = width + "px"; 

Run?

I have a feeling that your code fails every time you use a document. XXXXXX

'whitecontainer' is probably not a real element

+1
source

The problem is in this line.

document.getElementById ("whitecontainer"). Style.display

javascript cannot find the identifier "whitecontainer", so it throws a null exception and script breaks

+1
source

Source: https://habr.com/ru/post/987744/


All Articles