Return focus to the window that opened the popup

0

I have the following code that opens a popup window where I get some data from the database:

<script>
$("button").click(function(){
    window.open("janela.php", "_blank", "width=400, height=500");
});
</script>
In the popup window open, there is a button where the user clicks to send data to the main window ( janela.php ), which opened the popup : p>
<script>
$(".botao").click(function(){
    // outros códigos aqui que não vem ao caso
    $(window.opener.document).find("#status").text("Salvo!");
});
</script>

Everything works perfectly.

Doubt: How to do this so that when you click the opener button on the .botao , the focus pass from the popup to the main window (% with%)? That is, by clicking the popup switch to the background and the popup window is in the foreground on the screen.

  

Need something crossbrowser , which works in browsers   Chrome, Firefox, Safari, Opera, IE (at least 11) and Edge.

    
asked by anonymous 10.04.2018 / 22:31

3 answers

0

I found a solution for the mentioned browsers (speaking of more modern browsers, in April / 2018).

Before, it is necessary to set name to main window ( opener ), any name . Simply put in the main window script:

<script>
name = "principal";
</script>

And the code in popup for browsers:

- IE 11, Edge, Safari:

window.open('', window.opener.name);

- Chrome, Opera, Safari:

window.opener.focus();

- Firefox:

Only works with both lines at the same time:

window.open('', window.opener.name);
window.opener.focus();
    
25.04.2018 / 01:47
0

Disclaimer: I do not have enough points to comment, so I am responding here.

Test the following code, running along with your already done

<script>
$(".botao").click(function(){
$(window.opener.document).find("#status").text("Salvo!");
window.opener.focus(''); // joga o foco para janela que abriu a popup  
window.blur(); // tira o foco da popup
}
</script>
    
20.04.2018 / 19:55
0

An alternative would be for you to perform all the necessary actions and then perform a blur , a focus in the status field and finally close the popup.

$(".botao").click(function () {
    //...
    window.opener.$("#status").val("Salvo!");

    window.opener.$("#status").blur();
    window.opener.$("#status").focus();
    window.close();
});

However, in this case, you will be forced to close the popup and need to choose a field from the main window that will receive the focus.

I found a topic regarding "minimize" or switch focus between popups and parent window via JavaScript but apparently this is not possible.

    
23.04.2018 / 20:23