Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
how to check if the form has been refreshed/reloaded/closed in js

i know this question was asked before but i can't get this to work.

I have a .aspx which contains a function on the onunload body tag. This should refresh the parent page after the child page was closed.

At this point, i need to know if the page was refreshed/reloaded, because this logic will trigger the function call everytime and close my "child" page.

This is my code:

<script type="text/javascript">
    function refreshparent(){
        window.opener.location.reload(true);
        window.close();
    }
</script>

And this is what i want to do:

<script type="text/javascript">
    function refreshparent(){
      if(page.wasclosed){
        window.opener.location.reload(true);
        window.close();
      }else{
   //do nothing
      }
    }
    </script>

this a part of is my aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body style="background-color: #d6eceb" onunload="refreshparent()">
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

A way to communicate between multiple tabs or windows (that have the some origin, e.g. https://yourdomain.com/) is with the Broadcast Channel API. With it, you can open communications between the tabs and send data back and forth.

This might be helpful in your case to have the child page tell the parent window that it is going to close. Use the beforeunload event to trigger sending a message before the child window is closed.

Apply the following scripts to their respective files.

Script for your parent window.

This script opens a broadcast channel and listens for any incoming messages. Whenever the message is a string equal to 'refresh', then the page will reload.

<script type="text/javascript">
  const channelName = 'form-closing-channel';
  const channel = new BroadcastChannel(channelName);

  channel.addEventListener('message', ({ data }) => {
    if (data === 'refresh') {
      window.location.reload();
    }
  });
</script>

Script for your child window.

This script only sends a message to other open parent window(s) just before it is going to close. The message that is sent will be 'refresh', which should trigger the reload in the parent.

<script type="text/javascript">
  const channelName = 'form-closing-channel';
  const channel = new BroadcastChannel(channelName);

  window.addEventListener('beforeunload', () => {
    channel.postMessage('refresh');
  });
</script>

Important
Do note that all parent windows will respond to the received message. E.g. you have 3 open parent windows and 1 open child, closing the child will send 'refresh' to all of the parent windows and they will all reload.

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda