Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger a button click inside an iframe [duplicate]

This is the iframe code:

<div id="OutDiv" class="outerdiv">
    <iframe src="http://beta.sportsdirect.bg/checkout/onepage/" id="InnerIframe" class="FrameCSS" scrolling="no"></iframe>
</div>

Here is the HTMl code of the button which the iframe above is calling:

<button type="button"  id="SuperWebF1" title="Continue" class="button">Continue</button>

Here is the jQuery function that i use to trigger the click of the button when page is loaded:

 $('#SuperWebF1').trigger( "click" );

But this is working like that only when i place the jQuery code inside the source of button and the iframe is calling the button with the script.

I want to make an event which is clicking the button from outside the iframe. Is it possible ?

Thanks in advance!

like image 404
Venelin Avatar asked Dec 06 '14 13:12

Venelin


People also ask

How do I trigger a click event inside an iframe?

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.

Can we add button in iframe?

The Button is a widget for iFrame granting you an opportunity to build various buttons for different purposes on your website. There are tons of actions that can be taken after a user clicks the button.


1 Answers

NOTE: .contents() does not work if iframe's src points to cross domain page. More: Cross domain iframe issue and Get DOM content of cross-domain iframe

//execute code after DOM is ready
$(function() {

  //find iframe
  let iframe = $('#main_iframe');
  
  //find button inside iframe
  let button = iframe.contents().find('#main_button');
  
  //trigger button click
  button.trigger("click");
  
});
<!--Add jQuery library-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

If you need to manipulate another page inside iframe, search for official API or GET parameters. Example: Youtube Embed Video, Google Maps Embed Api

like image 75
Amit Garg Avatar answered Sep 21 '22 18:09

Amit Garg