Javascript Window Width - Resize

I am trying to understand that this is not working. He used to do growth instead of width, but I just can't get it to work. Can anyone point me in the right direction of the problem, maybe?

            function getWindowWidth() {
        var windowWidth = 0;
        if (typeof(window.innerWidth) == 'number') {
            innerWidth = window.innerWidth;
        }
        else {
            if (document.documentElement && document.documentElement.clientWidth) {
                windowWidth = document.documentElement.clientWidth;
            }
            else {
                if (document.body && document.body.clientWidth) {
                    windowWidth = document.body.clientWidth;
                }
            }
        }
        return windowWidth;
    }
    function Removewhensmall(id) {
        if (document.getElementById) {
            var windowWidth = getWindowWidth();
            if (windowWidth > 0) {


              var contentElement = document.getElementById(id);
                var contentWidth = contentElement.offsetWidth;

                if (windowWidth < 600) {
                    contentElement.style.display = 'none';


                }


            }
        }
    }
    window.onload = function() {

Removewhensmall('rightwrap');
    Removewhensmall('leftwrap2');

    }
    window.onresize = function() {

            Removewhensmall('rightwrap');
    Removewhensmall('leftwrap2');

    } 
+3
source share
1 answer
    if (typeof(window.innerWidth) == 'number') {
        innerWidth = window.innerWidth;
    }

must not be

    if (typeof(window.innerWidth) == 'number') {
        windowWidth = window.innerWidth;
    }

?

And further in the code

var contentWidth = contentElement.offsetWidth;

but contentWidthnever used again ...

In addition, you should use elseif()to prevent many nested if-clausules.

+3
source

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


All Articles