Is it possible to trigger a click event in cross domain iframe with javascript or jquery?
<html>
<head>...</head>
<body>
<div id='divA'>
<button type='button' id='buttonA'>Button</button>
</div>
<div id='divB'>
<iframe name='random'>
<div role='button' id='buttonB'></div>
</iframe>
</div>
</body>
</html>
Example : Trigger click event on div(#buttonB) when user click "buttonA"
You cannot detect click event in cross-domain iframe but one way is that you can detect focus on that iframe.
First click outside of iframe and then on iframe! You will see the alert.
window.focus(); //force focus on the currenct window;
window.addEventListener('blur', function(e){
if(document.activeElement == document.querySelector('iframe'))
{
alert('Focus Left Current Window and Moved to Iframe / Possible click!');
}
});
<iframe name='random'>
<div role='button' id='buttonB'></div>
</iframe>
You cannot detect click event in cross-domain iframe but one way is that you can detect focus on that iframe.
First click outside of iframe and then on iframe! You will see the alert.
window.focus(); //force focus on the currenct window;
window.addEventListener('blur', function(e){
if(document.activeElement == document.querySelector('iframe'))
{
alert('Focus Left Current Window and Moved to Iframe / Possible click!');
}
});
<iframe name='random'>
<div role='button' id='buttonB'></div>
</iframe>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With