Mohammad Alipour
Mohammad Alipour

Reputation: 323

How to check that a frame of frameset 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

Upvotes: 0

Views: 1407

Answers (2)

Dekel
Dekel

Reputation: 62576

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/

Upvotes: 3

StackOverMySoul
StackOverMySoul

Reputation: 2037

document.getElementById() returns null if the element doesn't exist, so the expression will be false, either add ! at the begining, or add your code in the else case

 if (!document.getElementById("f2")) {
}

or

 if (document.getElementById("f2")) {
} else {

}

Upvotes: 1

Related Questions