How to refresh a page after opening a new window

Asked

Viewed 122 times

1

I have a problem and do not know how to solve, I am creating a system that when you click on a link the browser opens a new window.

As in this example:

<a href="link" target="_blank">Veja esse vídeo</a>

When the user enters the link he is directed to a new window in the browser and a cookie is set in case the user goes back to the previous page it is different.

As in the example below:

if(isset($_COOKIE['riu'])):
    header('Location: ../riu.php');
endif;

But for this code to work the page needs to be updated to recognize the cookie and redirect the user to another page.

So what I want is that when the user enters the link that opens the video in another window, the link page is updated so that he runs the cookie script in php and redirects to the page "riu.php".

I’ve tried the second code:

<a href="link" target="_blank" onclick="Location.reload()">Veja esse vídeo</a>

But as shown above the page is refreshed without opening a new window.

  • 1

    I didn’t quite understand your need, you want to open the new page, set the cookie, and then want to that one page that arrow the cookie update, or the previous one that the user was on?

  • I want the previous page the user was on to be updated at the time the user enters the link that will open a new window.

1 answer

0


I do not believe it is the best way to do this, but it works using setInterval after opening the page:

var timer = setInterval(function() {   
        clearInterval(timer);  
        location.reload();
}, 100); 

To make things more practical, I’ve created a function that does this to Jquery that takes the attribute href of the link:

function abrirLink(event){
      event.preventDefault();
      var url = $(event.target).attr('href');
      window.open(url);
      var timer = setInterval(function() {   
        clearInterval(timer);  
        location.reload();
      }, 100); 
}
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  </head>
  <body>
    <a href="https://google.com" onclick="abrirLink(event)">Abrir</a>
  </body>
</html>

This way, just put onclick="abrirLink(event)" links you want to have this behavior.

It is also possible to omit the setInterval, and refresh the page right after the other opens:

function abrirLink(event){
          event.preventDefault();
          var url = $(event.target).attr('href');
          window.open(url);
          location.reload();
}

Browser other questions tagged

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