Pregunta

¿Hay alguna forma de saber si el usuario cierra una pestaña en un navegador web? Específicamente IE7, pero también FireFox y otros también. Me gustaría poder manejar esta situación desde nuestro código asp si se cierra la pestaña actual que contiene nuestro sitio web.

¿Fue útil?

Solución

Adjunte un " onbeforeunload " ; evento. Puede ejecutar código justo antes de que se cierre el navegador / pestaña.

Otros consejos

onbeforeunload también se llama en los siguientes eventos -

* Close the current browser window.
* Navigate to another location by entering a new address or selecting a Favorite.
* Click the Back, Forward, Refresh, or Home button.
* Click an anchor that refers the browser to another Web page.
* Invoke the anchor.click method.
* Invoke the document.write method.
* Invoke the document.open method.
* Invoke the document.close method.
* Invoke the window.close method.
* Invoke the window.open method, providing the possible value _self for the window name.
* Invoke the window.navigate or NavigateAndFind method.
* Invoke the location.replace method.
* Invoke the location.reload method.
* Specify a new value for the location.href property.
* Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the form.submit method.

Por lo tanto, si intenta cerrar la sesión del usuario si cierra la pestaña o la ventana del navegador, terminará desconectándose cada vez que haga clic en un enlace o envíe la página.

¿Document.unload no lo hace si usted?

Si necesita saber cuándo está cerrada la página en el lado del servidor, su mejor opción es hacer ping al servidor periódicamente desde la página (a través de XMLHttpRequest , por ejemplo). Cuando el ping se detiene, la página se cierra. Esto también funcionará si el navegador falla, se cerró o la computadora se apagó.

Como sugirió Eran Galperin, use onbeforeunload . En particular, devuelva una cadena, y esa cadena se incluirá en un diálogo de confirmación que permitirá al usuario elegir si desea abandonar la página o no. Cada navegador incluye un texto antes y después de la cadena que devuelve, por lo que debe probar en diferentes navegadores para ver qué se le presentará a un usuario.

scroll top