Unlike PHP, I cannot think of a safe and reliable way of splitting <script> tags to insert HTML content or stacks. Particularly from an aspect of un-escaped quote marks in your code.
Have you tried toggling class names like this?
function myScript() {
var weather;
var stacks_to_display = document.getElementsByClassName('myStack');
if (weather == "sunny") {
stacks_to_display.classList.toggle("sunny");
} else {
stacks_to_display.classList.toggle("overcast");
}
}
Depending on the condition returned, you can add or remove class names, using toggle, add or remove methods:
Then you can write CSS code to control which are shown, and which are hidden:
.myStack.overcast {
display: none;
}
Of course, the content for these stacks would always be visible in the page source code. This method only changes their visibility on the screen.
If you needed to physically remove all the code and content from the page, then server-side PHP is the way to go. And PHP would let you nest stacks properly within the “if” statements without too much difficulty.