Mohammad Alipour Mohammad Alipour - 2 months ago 12
Javascript Question

How to check that a frame of frameset is exists

I have a frameset that has 3 frames:

<frameset rows="124, *, 0">
<frame id="f1" scrolling="No" frameborder="0" src="" name="control">
<frame id="f2" frameborder="0" src="" name="main">
<frame id="f3" noresize frameborder="0" name="go">
</frameset>


I'm going to check if frame with id = "f2" exist?

I have tried :

<script>
if( document.getElementById("f2").contentDocument.documentElement.innerHTML !== null) {
alert('ok');
}
</script>


But not worked.
I know i should do something like:

document.getElementById("f2")


but need more info

Answer

Well - actually what you should do is only check if document.getElementById("f2") return something, however in order for this to work you must set the doctype of your document to frameset:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">

Otherwise the browser will not recognize the frame, and it will return nothing.

Check this fiddle:
https://jsfiddle.net/b8xg9y8u/