views:

866

answers:

2

I have found I can do the following:

if($('#notice', parent.frames['header'].document).length>0) { alert("It is here!"); }

to check for an item in another frame.
Is there a way to find out of the frame exists? Specifically I am looking to see if parent.frames['header'].document is there.

Is there a reliable way of doing this?

Update: Here's my frameset code:

<frameset rows="104,*,22" frameborder="NO" border="0" framespacing="0">
    <frame src="header.php" id="header" name="header" scrolling="no" title="Header and Menu" noresize>
    <frame src="main.php" title="Main content" name="main">
    <frame src="footer.php" name="footer" title="Footer" scrolling="NO" noresize>
</frameset>

I'm trying to make sure that I can access a div that lives inside of "header". The downside is that, in some cases, main is replaced with another frameset.

+1  A: 

Check the selector:

if ($("#myFrame").length) alert("It exists!");
Jonathan Sampson
He's doing that for an element within the frame, not the frame itself. Why on earth would you down-vote me because of that? It's completely unrelated.
Jonathan Sampson
I down-voted you because somebody ignorant up-voted you.
Josh Stodola
They upvoted because my example works. Checking the length of the selector when the selector looks for a frame is a valid option.
Jonathan Sampson
@Josh, I was checking the length of $("#myFrame") for zero. That's for the frame itself. The OP is checking for the length of an element within $("#myFrame"), which is completely different. I edited to provide a more simple example, which is perfectly acceptable.
Jonathan Sampson
`$("#myFrame").length;` vs. `$("#notice", $("#myFrame")).length;`
Jonathan Sampson
The first tell you if the frame exists, the second assumes it exists and tells you if the #notice exists. That's the difference.
Jonathan Sampson
With a upvote:downvote ratio like Josh's, he's better suited to troll a blog or forum instead of being an SO snob.
JMP
Oh, and by the way, since you couldn't figure it out yourself, this doesn't even work. First of all, frames have name attributes, not ID. So he'd hafta add an ID. Oh, and $() never returns false. Never. But I guess I was crazy and out of line for down-voting. And yet, my *correct* answer gets down-voted several times. Great community here!
Josh Stodola
This does not seem to work...it always tells me the frame exists even though in some cases it does not.
Jason
Jason, probably best to stick with my first suggestion, and checking .length on the selector. Sorry for changing it up on you ;)
Jonathan Sampson
+1  A: 

If you want to know if an element exists, you have to check the length property, see the jQuery FAQ:

if ($('#myFrame').length) {
  alert('#myFrame exists');
}

In your case I think you want to :

if ($('frame[name=header]', parent).length) {
  alert('frame exists');
}

Why check the length property?

Because if you pass a selector to jQuery that doesn't match anything, the returned result is a jQuery object, is not a falsy value (null, undefined, 0 or false), in an if statement the condition is evaluated to bool, and a non 'falsy' value is evaluated always to true:

if ($('#nonExisting')) {
  alert('always true');
}

// because 
!!$('#nonExisting') == true;  // and
!!'hello' == true;
!!0 == false;

I used !! as an example of a simple way of turning any expression into its boolean equivalent, what the if statement does behind the scenes...

Edit: Looking at your frameset markup, and assuming that you want to check if the header frame exists from the main page, you can easily do it by :

 if (parent.header !== undefined) {
   // frame exists
 }

Or simply:

 if (parent.header) {
   // frame exists
 }

Check this example:

CMS
I tried your second example if ($('frame[name=header]', parent).length) {and that didn't seem to work for me. Any suggestions ?
Jason
Awesome, thanks for the example
Jason