kit*_*kit 10 html javascript forms
出于某些原因,我需要使用本机HTML表单属性target="_blank"在新窗口中显示表单响应.
我有以下HTML表单myDomain.com:
<form target="_blank" method="POST" action="http://www.anotherDomain.com/somePage">
<input name="someFieldName" value="someValue"/>
<button type="submit">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
当用户单击时Submit,浏览器中将出现一个窗口/选项卡,我对表单响应没有访问控制权anotherDomain.com.
如何检测_blank窗户何时关闭?
window如果使用window.open(),我可以得到孩子的参考,但它不适合,因为它window.open()会被浏览器阻止.
这是我的解决方案:
<!-- set form target to a specific name -->
<form target="windowName" method="POST" action="http://www.anotherDomain.com/somePage">
<input name="someFieldName" value="someValue"/>
<button type="submit">Submit</button>
</form>
<script>
var childWindow;
$('form').get(0).onsubmit = function () {
// I think why window.open() here would not be blocked by browser because the function is attached to the HTML DOM and the event is triggered by user (mouse click)
childWindow = window.open('', 'windowName');
};
var checkWindowClose = setInterval(function () {
if (childWindow.closed) {
alert('Window is closed');
clearInterval(checkWindowClose);
} else {
// check the URL if childWindow has been redirected to somewhere
try {
// cross-origin exception will occur if the URL is in different domain
if (childWindow.document.URL == 'something') {
// ...
clearInterval(checkWindowClose);
}
} catch (err) {
// just ignore the error and do nothing
}
}
}, 1000);
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1652 次 |
| 最近记录: |