Javascript iframe表单提交后如何关闭窗口

Javascript iframe表单提交后如何关闭窗口,javascript,jquery,iframe,Javascript,Jquery,Iframe,我有一个带有iframe的空白页面,该页面显示来自我无法控制的外部域的表单。我正在尝试编写一点Javascript/jQuery,它将在表单提交后关闭窗口 这是我的密码: <body> <script> $('iframe input[type="submit"]').on('click', function () { window.close(); }); </script>

我有一个带有iframe的空白页面,该页面显示来自我无法控制的外部域的表单。我正在尝试编写一点Javascript/jQuery,它将在表单提交后关闭窗口

这是我的密码:

<body>
    <script>
        $('iframe input[type="submit"]').on('click', function () {
            window.close();
        });
    </script>

    <iframe src="SomeOtherDomain.com"></iframe>
</body>

$('iframe input[type=“submit”]”)。在('click',函数(){
window.close();
});
但当我点击按钮时,什么也没发生。我做错了什么

编辑:


我不确定它是否有区别,但看起来iframe调用了第二个iframe。。。我在iframe中有一个iframe。

只要您的iframe src来自同一个域,这就可以工作:

$(document).ready(function () {
var f = $('#myframe')[0];
var win = f.contentWindow;

//Created test environment in IFrame with ID "myframe"
$(win.document.body).append('<form><input type="submit" value="click me"/></form>');
$(win.document).on('submit', function () {
    setTimeout(function () { $(f).remove();  }, 1000)
});

});
$(文档).ready(函数(){
var f=$('#myframe')[0];
var-win=f.contentWindow;
//在ID为“myframe”的IFrame中创建了测试环境
$(win.document.body).append(“”);
$(win.document).on('submit',函数(){
setTimeout(函数(){$(f).remove();},1000)
});
});
只需删除元素,但请记住,您无法从不属于同一域的iframe获取contentDocument

工作解决方案:

我认为您需要提供一些时间(100毫秒)来提交表单数据。请通过调用deleteIframeTimer来尝试此操作

     function deleteIframeTimer(){
      var t = setTimeout("deleteIframe()", 100);
     }

    function deleteIframe() {
         var iframe = parent.document.getElementById("myframe");
         iframe.parentNode.removeChild(iframe);
    }

是否使用
window.open()
?@guest271314-否,我从未调用window.open()。如果您无法控制设置为
.src
的域,则无法确定是否可以将事件侦听器附加到
iframe
的用户操作
javascript
的问题是在
元素的
父级
窗口中,对吗?@guest271314-对。不幸的是,iframe源代码是另一个我无法控制的域。我相信你所要求的是不可能的。交叉起源将发挥作用。