Close the parent window on the iframe button.

I have an iframe without ID (generated by a third party). The IFrame is embedded on the landing page. When the submit button on the iframe is pressed, I would like to close the window or the landing page, because when the submit button is pressed, it will open a new tab.

function() {
    var iframe = document.getElementByTagName('iframe');
    var sbutton = document.getElementById("form_002b_ao_submit_href");
    iframe.sbutton.onclick = function() {
        window.unload();
    }
};

but it does not start.

+4
source share
2 answers
$('iframe').contents().find('body button').click(function (event) {
    event.preventDefault();
    $('iframe').remove();
});
0
source

If you want to close the main window after clicking the submit button, you can do something like the following.

Live preview

Link to test page loaded in iframe

HTML

<iframe src="http://s.codepen.io/larryjoelane/debug/dMoqPL"></iframe>

Javascript

//select the iframe element without an id
var submitButton = $("iframe").contents().find("#submit_button");

submitButton.on("click", function(event) {


  //if you need to prevent default actions(form submit for example)
  event.preventDefault();

  //close the window
  window.close();

  //debugging only
  //alert("iframe submit button click event fired");

});
0
source

All Articles