Check if window is already open window.open

前端 未结 5 2004
我寻月下人不归
我寻月下人不归 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);
    

提交回复
热议问题