最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect closing of window which opened with HTML form target="_blank" and different domain - Stack

programmeradmin2浏览0评论

For some reasons, I need to use the native HTML form attribute target="_blank" to display the form response in a new window.

I have the following HTML form in my myDomain:

<form target="_blank" method="POST" action="">
  <input name="someFieldName" value="someValue"/>
  <button type="submit">Submit</button>
</form>

When the user clicked Submit, a window/tab will appear in the browser, I have no access control to the form response from anotherDomain.

How to detect when the _blank window is closed?

I could get the child's window reference if using window.open(), but it is not suitable because window.open() would be blocked by browser.

For some reasons, I need to use the native HTML form attribute target="_blank" to display the form response in a new window.

I have the following HTML form in my myDomain.:

<form target="_blank" method="POST" action="http://www.anotherDomain./somePage">
  <input name="someFieldName" value="someValue"/>
  <button type="submit">Submit</button>
</form>

When the user clicked Submit, a window/tab will appear in the browser, I have no access control to the form response from anotherDomain..

How to detect when the _blank window is closed?

I could get the child's window reference if using window.open(), but it is not suitable because window.open() would be blocked by browser.

Share Improve this question edited Oct 29, 2015 at 2:39 kit asked Oct 28, 2015 at 10:15 kitkit 3253 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Here is my solution:

<!-- set form target to a specific name -->
<form target="windowName" method="POST" action="http://www.anotherDomain./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>

You have to use the javascript:Window.open method in you form action and not use target attribute.

By opening the new window in javascript you get an handle (myWin): var myWin = Window.open(,"_blank",

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论