I want to add a click event to an iframe
. I used this example and got this:
$(document).ready(function () {
$('#left').bind('click', function(event) { alert('test'); });
});
<iframe src="left.html" id="left">
</iframe>
But unfortunately nothing happens.
When I test it with another element (e.g. a button), it works:
<input type="button" id="left" value="test">
Two solutions:
:after
on a.iframeWrapper
elementpointer-events:none;
one the iframe1. Using
:after
Quite straightforward. The iframe wrapper has a
:after
(transparent) pseudo element with higher z-index - that will help the wrapper to register the click:2. Using
pointer-events:none;
Clicks are not handleable from outside the iframe from an external resource (if the iframe is not in your domain).
You can only create that function inside your 'called into iframe' page, not from within the iframe-hosting page.
How to do it:
iframe
into a diviframe
using CSSpointer-events:none;
jQuery
on your wrappingDIV
(theiframe
parent element)NOTA BENE:
pointer-events
:\You could attach the click to the iframe content:
Note: this will only work if both pages are in the same domain.
Live demo: http://jsfiddle.net/4HQc4/
The script would have to be ran entirely from the iframe. I would recommend a different method of calling content, such as php.
iframes aren't really worth the hassle.
I got it to work but only after uploading it to a host. I imagine localhost would work fine too.
outer
inner
The actual problem is that, the click event does not bind to the DOM of the iframe and bind() is deprecated, use
.on()
to bind the event. Try with the following codes and you will find the borders of the iframe clickable getting that alert.Demo of that Issue
How you should do is, create a function on iframe page, and call that function from that iframe page.