window.opener=null for new windows like popups, submit, form, with target=blank

154 views Asked by At

I had a Problem, but in this case I found some solutions here and on other websites I want to share with you.

Problem: window.opener functions of child pages were not working anymore since some security updates of the modern browsers about 2020 / 2021.

If you open a new link from your site via target="_blank" then in most cases the window.opener functions will not work anymore.

Solutions: see Answer below. I hope I could help other people with this solution.

I have searched many hours to solve these problems. I have even tried to allow the old behaviour via .htaccess but without success. If anybody has a working solution via .htacces please add a post or an answer.

1

There are 1 answers

0
XXL-Webdesign On BEST ANSWER

For simple href links you just have to add rel="opener" to get it working again.

<a href="https://myPage.com/childpage.php" target="_blank" rel="opener">Link to child page</a>

For some JavaScripts (one sample here) you can add setAttributs to your scripts

(...)
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "myChildPage.php");
// form.setAttribute("target","_blank");        // old
form.setAttribute("target", "formresult");      // new: setting form target to a window named 'formresult'
form.setAttribute("rel","opener");          // need to add because of security changes of newer browsers
(...)
document.body.appendChild(form);
form.submit();

For some Buttons with input type="submit" this is not as easy as the solutions before... The rel="opener" does not work within the button if you use formaction, formtarget (...)

Not working (rel="opener" in INPUT tag):

<form name="myForm" id="myForm" method="post">
(...)
<input type="submit" value="My Submit Button" formaction="childpage.php" formmethod="post" formtarget="_blank" rel="opener">
</form>

Working (rel="opener" in FORM tag)

<form name="myForm" id="myForm" method="post" rel="opener">
(...)
<input type="submit" value="My Submit Button" formaction="childpage.php" formmethod="post" formtarget="_blank" rel="opener">
</form>