Trigger function by pressing F5 (update)

Asked

Viewed 345 times

0

I am making an application in PHP that, through the selection of a data of the combobox related to vessel and a date field, I make a SELECT in the database returning some data that is inserted in a table.

I would like as soon as the user select the vessel (1) and for some case he press the button F5 (update) [2] the system calls a function.

I did a search for this feature in Javascript, but could not find.

To get more visual, follow my screen.

inserir a descrição da imagem aqui

1 answer

0


The Javascript event that is called before reloading the page by F5 is the beforeunload. I tested the code below and worked with Jquery 3.3.1. Note: some events like the alert are blocked at this event for security reasons.

window.addEventListener("beforeunload", function(event) {
   $.ajax({
       async: false,
       url: 'teste.php',
       data: { dados: "teste"},
       method: "POST"
   });
   return true;
});
  • One of those commands that browsers block would be ajax? I tried to merge in your function, an ajax with a type GET to get the condition I mentioned in the question but was unsuccessful.

  • @Almerindoabreu I improved the response and tested the code in Chrome. Ajax is not locked when using Jquery.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.