Uncaught TypeError: Cannot read property 'clientWidth' of null
Asked Answered
B

2

6

I have a responsive wordpress theme. The menu is coded to hide when the screen size is bellow 740. However it only does this correctly on the home page. If I go to any other page the menu collapses but it fails to hide and I get this error: "Uncaught TypeError: Cannot read property 'clientWidth' of null"

Here's the code, I have it being called in the header.php file of the theme:

var ww = document.body.clientWidth;
$(document).ready(function() {
    adjustMenu();


    $(".cat").click(function(e) { // cat class
        e.preventDefault();
        $(this).toggleClass("active");
        $(".sf-menu").toggle();
    });
});

function adjustMenu() {
    if (ww <= 740) { //change this to your breakpoint
        $('.sf-menu').hide();
        $(".cat").show();
        if (!$(".cat").hasClass("active")) {
            $(".sf-menu").hide();
        } else {
            $(".sf-menu").show();
        }
    } else {
        $('.sf-menu').show();
        $(".cat").hide();

    }
}


$(window).bind('resize orientationchange', function() {
    ww = document.body.clientWidth;
    adjustMenu();
});
Benzoyl answered 8/4, 2013 at 9:59 Comment(1)
Let me know if that solution works.. and Up vote if you find it useful :-)Valenti
V
6

I would say use $(window).width(); from jquery. It returns width of browser viewport Which is equivalent or I would say better alternative for traditional javascript.

Your code will look something like this. Check if it works.

var ww = $(window).width();
$(document).ready(function() {
    adjustMenu();


    $(".cat").click(function(e) { // cat class
        e.preventDefault();
        $(this).toggleClass("active");
        $(".sf-menu").toggle();
    });
});

function adjustMenu() {
    if (ww <= 740) { //change this to your breakpoint
        $('.sf-menu').hide();
        $(".cat").show();
        if (!$(".cat").hasClass("active")) {
            $(".sf-menu").hide();
        } else {
            $(".sf-menu").show();
        }
    } else {
        $('.sf-menu').show();
        $(".cat").hide();

    }
}


$(window).bind('resize orientationchange', function() {
    ww = document.body.clientWidth;
    adjustMenu();
    });
    function adjustMenu() {
        if (ww <= 740) { //change this to your breakpoint
        $('.sf-menu').hide();
        $(".cat").show();
        if (!$(".cat").hasClass("active")) {
            $(".sf-menu").hide();
        } else {
            $(".sf-menu").show();
        }
    } else {
        $('.sf-menu').show();
        $(".cat").hide();

    }
}


$(window).bind('resize orientationchange', function() {
    ww = $(window).width();
    adjustMenu();
});
Valenti answered 8/4, 2013 at 10:10 Comment(0)
L
9

try adding your script at the end of the document. The reason is that, a the beginning your document width is zero, because you content is not loaded yet, so there's nothing to display and so, you width is zero

Lamelliform answered 23/6, 2013 at 17:56 Comment(2)
Thanx.!!So easy & simple solution.Eyed
omg, tnx sir, it was so easy but i was stock in this ez problem, shame on me =)))Grievous
V
6

I would say use $(window).width(); from jquery. It returns width of browser viewport Which is equivalent or I would say better alternative for traditional javascript.

Your code will look something like this. Check if it works.

var ww = $(window).width();
$(document).ready(function() {
    adjustMenu();


    $(".cat").click(function(e) { // cat class
        e.preventDefault();
        $(this).toggleClass("active");
        $(".sf-menu").toggle();
    });
});

function adjustMenu() {
    if (ww <= 740) { //change this to your breakpoint
        $('.sf-menu').hide();
        $(".cat").show();
        if (!$(".cat").hasClass("active")) {
            $(".sf-menu").hide();
        } else {
            $(".sf-menu").show();
        }
    } else {
        $('.sf-menu').show();
        $(".cat").hide();

    }
}


$(window).bind('resize orientationchange', function() {
    ww = document.body.clientWidth;
    adjustMenu();
    });
    function adjustMenu() {
        if (ww <= 740) { //change this to your breakpoint
        $('.sf-menu').hide();
        $(".cat").show();
        if (!$(".cat").hasClass("active")) {
            $(".sf-menu").hide();
        } else {
            $(".sf-menu").show();
        }
    } else {
        $('.sf-menu').show();
        $(".cat").hide();

    }
}


$(window).bind('resize orientationchange', function() {
    ww = $(window).width();
    adjustMenu();
});
Valenti answered 8/4, 2013 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.