Check if window is already open window.open

前端 未结 5 2017
我寻月下人不归
我寻月下人不归 2020-12-01 16:15

I have a html page. In the body of the page I am calling onload event which calls javascript function to open a pop up window. here is the code:



        
5条回答
  •  一向
    一向 (楼主)
    2020-12-01 16:31

    To open a window and keep a reference to it between page refresh.

    var winref = window.open('', 'MyWindowName', '', true);
    if(winref.location.href === 'about:blank'){
        winref.location.href = 'http://example.com';
    }
    

    or in function format

    function openOnce(url, target){
        // open a blank "target" window
        // or get the reference to the existing "target" window
        var winref = window.open('', target, '', true);
    
        // if the "target" window was just opened, change its url
        if(winref.location.href === 'about:blank'){
            winref.location.href = url;
        }
        return winref;
    }
    openOnce('http://example.com', 'MyWindowName');
    

提交回复
热议问题