2015-10-28 3 views
9

で開いたウィンドウの開閉を検出し、私は新しいウィンドウでフォームの応答を表示するには、ネイティブのHTMLフォームの属性target="_blank"を使用する必要があります。はいくつかの理由から、HTMLフォームターゲット=「_ブランク」と、別のドメイン

私は私のmyDomain.comに次のHTMLフォームを持っている:

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

ユーザーがSubmitをクリックすると、ウィンドウ/タブがブラウザに表示されます、私はanotherDomain.comからフォーム応答にはアクセスコントロールを持っていません。 _blankウィンドウが閉じているときを検出する方法

window.open()を使用している場合、私は子供のwindowの参照を取得することができますが、window.open()がブラウザによってブロックされるので、それは適切ではありません。あなたはjavascriptを使用する必要が

答えて

2

は私のソリューションです:

<!-- 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> 
0

:あなたにWindow.openメソッドは、アクションを形成し、ターゲットの属性を使用していません。あなたがハンドル(myWin)を取得するJavaScriptで新しいウィンドウで開くことにより

: するvar myWin = Window.open( "_空白" を、ここで

+0

はあなたのコードですコンプリート? –

関連する問題