0

I have a application where I am doing recording through a third party device using it's API on my page. Recording is started and stopped through a asp.net web service..

I am able to start and stop the recording through JavaScript in normal cases and the functionality works fine in all browsers.

But, once accidentally one user without stopping the video, closed the browser in which case the recording could not be stopped.. and it went on recording..

So, please suggest me a way so that I can pass a request to the server to stop the recording automatically when the browser is closed..

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rahul Ranjan
  • 1,028
  • 1
  • 10
  • 27

2 Answers2

2

You can't.

You can show a standard dialog or do a few fast not interactive operations like writing in localStorage, by using onBeforeUnload, but you can't make a request (in most browsers).

You have to handle the case of browser closing (or network lost, or any incident) server side.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

I asked this question a while ago for closing popups and this website helped me out.

var win = window.open('http://www.google.com', 'google','width=800,height=600,status=0,toolbar=0'); 
var timer = setInterval(function() { 
if(win.closed) {
    clearInterval(timer);
    alert('closed');
}
}, 1000);

Replace the code in the function with your own.

Good luck!

EDIT: I must however say, I didn't tried this in all browsers yet, so you'll have to test that.

Boyye
  • 301
  • 1
  • 9
  • This may be helpful. But OP really should handle network or browser failure cases server side. This wouldn't work, for example, if OP closes parent window. – Denys Séguret Sep 21 '12 at 12:16
  • @dystroy ..Then there might be a way to disable the browser close button for a certain period..isn't it there?? – Rahul Ranjan Sep 21 '12 at 14:08
  • 1
    @RahulRanjan Everything which might block the user wanting to close a window is impossible. – Denys Séguret Sep 21 '12 at 14:31
  • @dystroy okk...great..but then, do you have any suggestion to implement this functioanlity at server side i.e. on web service side to check if the user has closed the browser or network lost between service and browser?I know I am getting away from the topic but if only you have the answer,it will prevent me to open an other question.. – Rahul Ranjan Sep 21 '12 at 14:36
  • You might **1)** use a websocket and detect when it's closed **2)** regularly send "i'm alive" messages from the browser to the server using ajax. I'm afraid there is no one line solution for this problem. – Denys Séguret Sep 21 '12 at 14:38