Removing a child window of a child form

I am dealing with an old developer site and code.

There is a GLOBAL print function that the guy wrote, which basically feels for any new window that opens (from any method) and "href.match" with the domain name ... the script then applies the print stylesheet if it needs to be and run window.print.

All this is done from a global script that is on every page and contains some other functions.

I am tired of writing cases for every page added that I want to avoid this function. Also, if I write a NOT clause for a specific page, any subsequent page opened in the domain of the child window will then receive this print function. A.

Is there any way to “impose” inheritance in this new window? Basically, to make this window NOT a child of the parent who spawned it?

    addEvent(window, 'load', function () {
    var printBtn = document.getElementById('print-page');
    if (window.opener && window.opener.location.href.match('domainnamehere')) {
        var printCSS = document.createElement('link');
        var a = document.getElementsByTagName('a');
        printCSS.href = 'css/print.css'
        printCSS.setAttribute('type', 'text/css');
        printCSS.setAttribute('rel', 'stylesheet');
        document.getElementsByTagName('head')[0].appendChild(printCSS);

        for (var i = 0; i < a.length; i++) {
            a[i].href="";
            a[i].onclick = function () { return false; };
            a[i].style.cursor = "default";
        }

        window.print();
    } else if (printBtn){
        printBtn.onclick = function () {
            var printWindow = window.open(window.location, 'printwindow', 'resizable,width=800,height=800,scrollbars');

            return false;
        };
    }
});
+3
source share
2 answers

The property openeris that gives access to the child window, so if you want to disable it, all you have to do is set its value to null. In doing so, I always copy it to another property before cancellation, if you need it for other reasons ...

if(bustInheritance) {
  window.oldOpener = window.opener;
  window.opener = null;
}
+5
source

Hacky, but can do the trick:

var win_onload = window.onload;
// replace it with a function that does nothing
window.onload = function () { };

... open your window ...

// put the event handler back
wndow.onload = win_onload;
+1
source

Source: https://habr.com/ru/post/1791298/


All Articles