How do I keep the child window visible when the parent window is focused?

Asked 2 years ago, Updated 2 years ago, 21 views

In javascript, open the child window from the parent window and focus() into the parent window.
At this time, you are looking for a way to keep the child window on the front.
The browser is IE11.

Specifically, when opening a child window, setTimeout focuses on the parent window after 3 seconds.
At this time, the child window loses focus, so use window.onblur or document.onfocusout to
I thought that if you focus on the child window, it will continue to be displayed in front of you.
Therefore, I made the parent and child windows as follows.

Parent window (parent.html):

<html>
<body>
<a href="javascript:void(0);"onclick="openCF();">Open child window</a>
<script type="text/javascript">
function openCF(){
    open("./child.html", "_blank", 'width=200, height=200');

    setTimeout(function(){
        window.focus();
    },3000);
}
</script>
</body>
</html>

Child window (child.html):

<html>
<body>
<p>Child Window</p>
<script type="text/javascript">
    window.focus();    
    window.onblur=getFocus1;
    document.onfocusout=getFocus2;

    function getFocus1(){
        window.focus();
    }

    function getFocus2(){
        window.focus();
    }
</script>
</body>
</html>

If you do this on IE11, three seconds after opening the child window, the child window is unexpectedly
It hides behind the parent window.
However, if you select the parent window behind you when the child window is in front of you,
The child window continues to appear unhidden.
Would it be possible to prevent the child window from being hidden even if I focus on the parent window?

add

Modalize in window.open -Qiita

Using the above URL as a reference, I tried to check if the child window is focused every certain period of time, and if it is out of focus, I tried to focus it.
The parent.html is as follows.

<html>
<body>
<a href="javascript:void(0);"onclick="openCF();">Open child window</a>
<script type="text/javascript">
function openCF(){
    varchildWindow=open("./child.html", "_blank", 'width=200, height=200');

    setTimeout(function(){
        window.focus();
    },3000);

    var interval = setInterval(function()
    {
        // focus on the child screen
        if(childWindow!=null&&!childWindow.document.hasFocus())
        {
            childWindow.focus();
        }
    },100);
}

</script>
</body>
</html>

Use setInterval to check the child window every 0.1 seconds and focus() if it is not in focus.

Now that I've done it, the child window seems to be in focus, but the child window still didn't come forward.
I'm looking further to see if focusing doesn't necessarily match the front of the window.

I would appreciate your continued response.

javascript

2022-09-29 22:29

2 Answers

This kind of behavior compromises usability and is increasingly restricted by browsers.
Now that mobile is increasing, it's not a good idea to use windows.
Pop-up displays should be done within one tab as much as possible.


2022-09-29 22:29

If you focus on the child Windows on the open side of Windows, it will be the desired behavior.

partial.html

function openCF(){
    varchildWin=open("./child.html", "_blank", 'width=200, height=200');

    setTimeout(function(){
        childWin.focus();
        setTimeout (arguments.callee, 1000)
    },3000);
}

The sample focuses periodically, but the child Windows focus state is
child Windows focus state. Set the input[hidden] area and look at the value and it's out of focus.If you try to focus when you can, it will work a little more properly.

However, this method does not focus the child when the IE of the parent is minimized.
This is an unavoidable problem at the Windows API level, so to avoid it,
You have no choice but to create your own program.

How to focus external application windows

Also, if there are security-isolated applications such as opening a command prompt with administrator privileges or remote desktop connections, those apps will be the top priority, so I don't think they will work as expected.
As long as it's Windows, there's nothing you can do about it.


2022-09-29 22:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.