单击iframe按钮时关闭父窗口

Close parent window on iframe button click

本文关键字:窗口 iframe 按钮 单击      更新时间:2023-09-26

我有一个没有ID的iframe(由第三方生成)。iframe 嵌入在登录页面上。当单击iframe上的提交按钮时,我希望关闭窗口或登录页面,因为单击提交按钮时,它将打开一个新选项卡。

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

但它不会被触发。

$('iframe').contents().find('body button').click(function (event) {
    event.preventDefault();
    $('iframe').remove();
});
如果您想在

单击提交按钮后关闭主窗口,那么您可以执行以下操作。

实时预览

链接到在 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");
});