K
K
Kyber_Ded2021-06-08 14:03:43
JavaScript
Kyber_Ded, 2021-06-08 14:03:43

How to catch page refresh (reload) event?

There are 2 tabs.
When one is closed, by the onunload event, it transfers data to another.
But there's a problem. onunload also works on page reloads.

The question arose, how to catch the reboot event? Through keydown and the key code will not work, because the user can also click on the page reload button in the browser.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey delphinpro, 2021-06-08
@Kyber_Ded

There is no such event. And I personally do not see a solution to the problem, as it is posed.
But you probably don't need to pass data on close. It may be suitable to synchronize data between open tabs of your domain.
Look here https://habr.com/ru/company/rambler_and_co/blog/422545/

M
mikhailivanko, 2021-06-08
@mikhailivanko

document.addEventListener("DOMContentLoaded", function(){
 //if IsRefresh cookie exists
 var IsRefresh = getCookie("IsRefresh");
 if (IsRefresh != null && IsRefresh != "") {
    //cookie exists then you refreshed this page(F5, reload button or right click and reload)
    //SOME CODE
    DeleteCookie("IsRefresh");
 }
 else {
    //cookie doesnt exists then you landed on this page
    //SOME CODE
    setCookie("IsRefresh", "true", 1);
 }
})

I think it's clear how to add and remove cookies.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question