0

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

But i get an error:

"TypeError: e is null"

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 || e.innerHTML!="\n"); }

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

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

3 Answers3

0

Referencing your jsfiddle, this seems to be an issue with the doctype. Changing the DTD under Fiddle Options to "HTML 4.01 Frameset" seems to correct the issue.

It seems that the frame elements do not exist in the document otherwise. getElementsByName does not return them, and I tried changing things to use ID and had the same issue.

ryachza
  • 4,460
  • 18
  • 28
0

Obviously the body of the contentDocument of content3 is not there (null) (this is the e in the error msg).

Maybe you can check if your frame is empty by

function isEmpty(frame) {
 return null === frame.contentDocument.body;
}

and call it as:

isEmpty(document.getElementsByName("content3")[0])

or so..

fast
  • 885
  • 7
  • 15
-1

The typeerror you describe happens when you dereference an undefined value.

In this case I assume getElementsByName(content3) or the [0] afterwards returns an undefined value on which you then try to call .contentDocument which causes the error.

I would suggest using your debugger, check if

document.getElementsByName("content3") 

or

document.getElementsByName("content3")[0]

is defined.

There are some good questions about how to find if an element is undefined. like this one

Community
  • 1
  • 1
Joeblade
  • 1,735
  • 14
  • 22
  • @error404username_not_found `isEmpty()` is called using 'contentX' as parameters. I' still suggest you share more of the code. – lockedz Aug 19 '15 at 13:30