April 2, 2012

Click in iframe register parent window

Question by Zolka

I have a webpage where I have an Iframe. When someone clicks something in the Iframe I want to detect it in the parent window.

I see there are similar threads in the forum but no one solved my issue.

Zolka

Answer by GregL

You need to use the jQuery .contents() method to retrieve the contents of the iFrame, and then search within that for your .click1 elements.

See the following jQuery code (works in v1.7 up):

$('#1_iframe').load(function() {
    $(this).contents().find('#myGrid').on('click', '.click1', function() {
        alert('Click detected!');
    });
});

EDIT: Wrapped it in a .load() event handler.

Answer by Starx

Attach a event on the iframe

$(document).click(fucntion(e) {
   var targ = e.currentTarget();

   //Now send this to the parent function
   parent.NewClick(targ);
});

Now the NewClick() function in the parent window, will know which button was clicked.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!