Javascript - checking if frame is empty

I tried to check if the frame "content1" and "content3" are empty or not and then resizing them.

But the size just changes every loop

window.setInterval(function () {
check();
}, 50);

function check() {

    var content1 = document.getElementsByName("content")[0].contentDocument.body;
    var content3 = document.getElementsByName("content3")[0].contentDocument.body;

    if (isEmpty(content3)) {
        if (isEmpty(content1)) setProperties("0px, *, 0px");
        else setProperties("35%, *, 0px");
    } else setProperties("25%, 40%, 35%");

    window.localStorage.clear();
}

function isEmpty(e) {
    return (e.offsetWidth != 0);
}

function setProperties(value) {
    document.getElementsByTagName("frameset")[1].cols = value;
}

https://jsfiddle.net/ecytve7w/8/

Upvotes: 0

Views: 205

Answers (1)

Pointy
Pointy

Reputation: 413757

Well, that error means what it says: e is null, so e.offsetWidth will throw the exception. Just check e also:

return !!(e && (e.offsetWidth != 0 || e.innerHTML != "\n"));

The !! makes it such that the function always returns a boolean true or false result, which isn't necessary but I like to do that with isFoo() functions just to be nice.

Why would e be null? In this case it'd be because the code didn't find either the "content" or "content3" elements on the page.

Upvotes: 2

Related Questions