Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to close current browser window and open new window using JavaScript

I am trying to close the current windows brower and open a new one. Currently, it opens a new window on each click which is not correct.

Not sure where am I doing wrong in this?

Can someone point me right direction?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<div id="oWrapper" style="display: none;">
    <div id="oIn"></div>
</div>

 $(document).ready(function () {          

        var isIE = false || !!document.documentMode;

        if (isIE) {
            var tURL = 'microsoft-edge:' + '@Url'.replace(/amp;/g, '');
            var openE = document.querySelector('#oIn');
            openE.href = 'microsoft-edge:' + document.URL;
            var oWrapper = document.querySelector('#oWrapper');
            oWrapper.style.display = 'block';
            window.location.replace(tURL);
        }
        else {           
            var params = [
                'height=' + screen.height,
                'width=' + screen.width,
                'scrollbars=yes',
                'resizable=yes',
                'location=no'
            ].join(',');

            var Obj;

            function ModalPopUp() {
                Obj = window.open('@Url'.replace(/amp;/g, ''), "_blank", params);
                Obj.moveTo(0, 0);

                Obj.focus();
                LoadModal();
            }

            function LoadModal() {
                var Div = document.getElementById("oIn");
                Div.style.display = "block";
            }

            function OnUnload() {                 
                if (false == Obj.closed) {
                    Obj.close();
                }
            }
            window.onunload = OnUnload;
            ModalPop();
        }
    });

>Solution :

It seems like you only try to Obj.close upon window.onunload, instead of before a new modal is opened.

Try:

function ModalPopUp() {
    if (Obj) Obj.close(); // close existing modal if it exists
    
    Obj = window.open('@Url'.replace(/amp;/g, ''), "_blank", params);
    Obj.moveTo(0, 0);

    Obj.focus();
    LoadModal();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading