Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggering an event handler on an element inside iframe from parent frame

I have a html page. Inside it I use jQuery to attach click event on a link. Then I use iframe to reference this html page from another page. However I could not trigger the event attached on that link with js (when I click the link with the mouse, the event gets triggered with no problem). How can I trigger that event from parent frame? I have tried the following but it does not work:

var frame = $('#mainFrame'); // the iframe I am trying to access
var link = $('a.btn', frame.contents()); // the element is found correctly
link.trigger('click');  // nothing happens. 
var e = link.data('events');  // e is undefined. 
like image 368
YANG Lei Avatar asked Aug 20 '12 03:08

YANG Lei


People also ask

Can I add event listener to iframe?

You can also add event listeners that will execute in response to certain player events, such as a player state change. This guide explains how to use the IFrame API. It identifies the different types of events that the API can send and explains how to write event listeners to respond to those events.

Which method attaches an event handler to an element?

The addEventListener() method attaches an event handler to the specified element. The addEventListener() method attaches an event handler to an element without overwriting existing event handlers. You can add many event handlers to one element.

How do I click an iframe event?

Answer: Use the jQuery contents() method If you try to detect or capture a click event inside an iframe simply using jQuery click() method it will not work, because iframe embed a web page within another web page. However, you can still do it on the same domain utilizing the jQuery contents() and load() method.


1 Answers

Just place onload() event inside the iframe tag.

<iframe src="http://myURL.com" onload="myFunction()">
like image 54
Dogwood LX Avatar answered Nov 15 '22 21:11

Dogwood LX