Reputation: 1583
My webapp can be run "by itself" or opened in a modal window by another webapp...
When the user closes the window I must clean some data so I catch the event of closing the window and I redirect to a certain action like this:
<script language="JavaScript">
window.onbeforeunload = function() {
if ((window.event.clientX < 0) || (window.event.clientY < 0) || (window.event.clientX < -80) || window.event.altKey == true) { // close button or ALT+F4
window.location.assign('<%=request.getContextPath()%>/borraSemaforo.do');
}
};
</script>
This works when the webapp is runing in a non-modal window, but not inside a modal window.
I have no control over the application that opens mine in a modal window so I can not make any change to the way the window is opened...
Any ideas?
Upvotes: 0
Views: 3368
Reputation: 1912
For cases where your application loads in a modal window:
One way is:
You can create an hidden iframe in the window in your onbeforeunload event handler. In the iframe put the url of the action.
Another way -
Create a small modal window pointing to the url path. In the response of that url path send a small javascript to just close the modal window.
Code for opening an iframe - basic js
var i = document.createElement('iframe');
i.style.display = "none";
var d = new Date();
i.id = d.getTime();
i.onload = function(){
/*Task complete*/
alert('frame loaded')}
i.name = "exp" + d.getTime();
i.src = window.location.protocol + "//" + window.location.host + '<path>';
document.body.appendChild(i);
Upvotes: 1