Search code examples
javascriptjqueryhtmlframeframeset

Can't find frames children


I'm maintaining an old site that uses frameset/frames.

I wan't to attach a handler to the main frame so when the user clicks the body somewhere an alert will pop up.

I've been banging my head for a while now, but I now feel like I'm so close I can taste it, yet I still can't figure out the problem. I'm using jquery 1.5.1.

$(function () {
    $($('frame[name="main"]')).ready(function () {
        console.log($('frame[name="main"]')); // this is a success (I can view it in firebug
        console.log($('frame[name="main"]').find('body')); // fails (nothing is found)
    });
});

Here are the frames

<frameset cols="190,*" frameborder="no" border="0" framespacing="0" framepadding="0" marginwidth="0" marginheight="0">
    <frame name="head" src="someHTMLPage1.html" scrolling="no" noresize frameborder="no" border="0" framespacing="0" framepadding="0" marginwidth="0" marginheight="0">
    <frame name="main" src="someHTMLPage2.html" scrolling="auto" frameborder="no" border="0" framespacing="0" framepadding="0" marginwidth="0" marginheight="0">
</frameset>

Solution

  • Thanks Arturo,

    Your solution helped but wasn't perfect. Here's what I did.

    $(function () {
    
        $($('frame[name="info"]')).load(function () {
            var doc = window.frames["info"].document;
            doc.onclick = function () { alert('something') };
        });
    });
    

    I had to use .load instead of .ready in order for this to work.

    This worked in IE 7/8/8-compatibility-mode and FF4 and Chrome 12 (probably works in earlier versions of FF and Chrome, I just didn't test)