Redirect a page with javascript when opening a new tab

Asked

Viewed 581 times

1

I need to redirect a page with javascript after submitting a form, the problem is that this form opens in a new tab and after opening this new tab I need to redirect the original tab to a new address.

Using the code below I can’t do it:

$("#gerarBoletoForm").on('submit', function(){
    window.location.replace('https://www.site.com.br/');
});

Not even if I use Location.href or Location.assign.

Is there any way to do that?

2 answers

1

The object window.opener gives you access to the "mother" tab. With window.opener.location.href you will be able to control her url.

  • The problem is that I do not control the new tab, it is the bank tab of Brazil where the ticket is generated...

  • When the ticket is generated the window closes? If yes, it is possible to catch this event.

  • Don’t close @lfarroco

  • The problem is that since the target site does not have the same origin (it is on the BB website), you cannot access the content by security (cross-origin). When this BB form is sent, is any of your servers notified? If yes, it would be possible to check through the original window.

1


Based on the information you gave me I was able to solve the problem.

First I set the target of my form in a new window created by me (and not the _target of the form) after that I opened this new window and assigned it to a variable and then submit the form.

After that I can use opener.location.href without problems. Below I leave the solution code:

$("#gerarBoletoForm").on('click', function(){
    document.gerarBoletoForm.target = "novaJanela";
    var boletoWindow = window.open("","novaJanela","toolbar=0");
    document.gerarBoletoForm.submit();

    boletoWindow.opener.location.href = "https://www.site.com.br/";
});

From this it was possible to redirect the "mother window" without problems. Thanks for all the help!

PS: I do not know if it is the best solution but it was possible to solve in this way.

Browser other questions tagged

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