Check if window is already open window.open

前端 未结 5 1992
我寻月下人不归
我寻月下人不归 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:26

    When you move on another page (on the same domain), you can re-set the window.open variable with popup page like this :

    https://jsfiddle.net/u5w9v4gf/

    Step to try :

    1. Click on Run (on jsfiddle editor).
    2. Click on Try me (on preview).
    3. Click on Run to move on another page, the variable will be re-set.

    Code :

    window.currentChild = false;
    
    $("#tryme").click(function() {
        if (currentChild) currentChild.close();
        const child = window.open("about:blank", "lmao", 'width=250,height=300');
        currentChild = child;
    
        //Scrope script in child windows
        child.frames.eval(`
        setInterval(function () {
            if (!window.opener.currentChild)
                window.opener.currentChild = window;
        }, 500);
      `);
    });
    
    
    setInterval(function() {
    console.log(currentChild)
        if (!currentChild || (currentChild && currentChild.closed))
            $("p").text("No popup/child. :(")
        else
            $("p").text("Child detected !")
    }, 500);
    
    0 讨论(0)
  • 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');
    
    0 讨论(0)
  • 2020-12-01 16:36
    newWindow = window.open('abc.html','com_MyDomain_myWindowForThisPurpose','height=960px,width=940px');
    

    Give the window a name. Basing the name on your domain like this, prevents the chances of you picking a name someone else happened to choose.

    Never make up a name that begins with _, those are reserved for special names the browser treats differently (same as with the "target" attribute of anchor elements).

    Note that if the window of that name was opened with different options (e.g. different height), then it'll keep those options. The options here will only take effect if there is no window of that name, so you do create a new one.

    Edit:

    Note that the "name" is of the window, not of the content. It doesn't affect the title (newWindow.document.title will affect that, as of course will code in abc.html). It does affect other attempts to do stuff across windows. Hence another window.open with the same name will reuse this window. Also a link like <a href="def.html" target="com_MyDomain_myWindowForThisPurpose">clicky!</a> will re-use it. Normal caveats about browsers resisting window-opening in various scenarios (popup-blocking) apply.

    0 讨论(0)
  • 2020-12-01 16:37

    You can check if the window is open or closed by re-assigning a reference to it when it closes. Example:

    var newWindow;
    var openWindow = function(){
        newWindow = newWindow || window.open('newpage.html');
        newWindow.focus();
        newWindow.onbeforeunload = function(){
            newWindow = null;
        };
    };
    
    0 讨论(0)
  • 2020-12-01 16:41

    Use the "closed" property: if a window has been closed its closed property will be true. https://developer.mozilla.org/en-US/docs/Web/API/Window/closed

    0 讨论(0)
提交回复
热议问题