cómo llamar a una función antes de recargar la página jquery

I have a few links in my page and I want to call a function when the page is trying to reload.

Lo intenté

$(window).unload(function() {
  alert('Handler for .unload() called.');

No está trabajando.

preguntado el 22 de mayo de 12 a las 11:05

"Not working" in what browser? -

@raina77ow -> google chrome......... -

@KanishkaPanamaldeniya: what actions(s) do you want to perform onunload? Also, bear in mind unload is called: "... when the user navigates away from the page... clicked on a link... typed in a new URL... The forward and back buttons [and] ... closing the browser window [and] ... a page reload will first create an unload event." (Source). -

3 Respuestas

Consola: Blocked alert('Handler for .unload() called.') during unload. (en cromo)

I assume this is blocked for user-experience reasons.

Si intentas console.log('Handler for .unload() called.'); the call is made successfully.

Editar: ver $ (ventana) .unload no se activa

contestado el 23 de mayo de 17 a las 13:05

puedes probar:

window.onbeforeunload = function() {


contestado el 22 de mayo de 12 a las 11:05

hm the problem in this code is iti is asking the user to leave or stay in the page , i don't want , i want a function to be called , but the user should not know that . acctually i am trying to pass some values to the server and save in the session for future use - Kanishka Panamáldeniya

window.onbeforeunload = unloadMessage;

   function unloadMessage() {
           //do your codeing here...

Respondido 14 Feb 13, 12:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.