2015-10-28 10 views
9

Per alcuni motivi, è necessario utilizzare l'attributo di modulo HTML nativo target="_blank" per visualizzare la risposta del modulo in una nuova finestra.Rileva chiusura della finestra aperta con modulo HTML target = "_ blank" e dominio diverso

Ho il seguente form HTML nel mio myDomain.com:

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

Quando l'utente fa clic Submit, una finestra/scheda apparirà nel browser, non ho il controllo di accesso alla risposta modulo da anotherDomain.com.

Come rilevare quando la finestra _blank è chiusa?

Potrei ottenere il riferimento window del bambino se si utilizza window.open(), ma non è adatto perché window.open() sarebbe bloccato dal browser.

risposta

2

Ecco la mia soluzione:

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

Devi usare il metodo javascript: Window.open nella tua azione modulo e non utilizzare l'attributo target.

Aprendo la nuova finestra in javascript si ottiene una maniglia (myWin): var = myWin Window.open ("_ blank",

+0

è il codice completare? –

Problemi correlati