Javascript 如何让子窗口保持在顶部?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13965584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:17:16  来源:igfitidea点击:

How to make child window stay on top?

javascriptfirefoxalways-on-top

提问by Andrew Fox

I am using window.opento open a child window from the parent window. I want the child window to stay on top so the user can refer to it while making an entry in the parent window. Can this be done? I'm using Firefox at the moment, but it would be a bonus if it worked in all browsers.

我正在使用window.open从父窗口打开子窗口。我希望子窗口保持在顶部,以便用户在父窗口中进行输入时可以引用它。这能做到吗?我目前正在使用 Firefox,但如果它适用于所有浏览器,那将是一个奖励。

采纳答案by 3dgoo

How about using a popup divinstead of opening a new window?

如何使用弹出 div而不是打开一个新窗口?

回答by jikey

this popup layer is also good: DOMWindowDemo.

这个弹出层也不错:DOMWindowDemo

回答by John Page

I wrestled with this for a long time. It seems to be a bug in FF, but I noticed that after the new window opens, if I click on it, it does get focus and comes to the top. However calling window.focus() on it didn't work, so I guessed it was happening too early.

我为此纠结了很长时间。这似乎是 FF 中的一个错误,但我注意到在新窗口打开后,如果我单击它,它确实会获得焦点并到达顶部。然而,在它上面调用 window.focus() 不起作用,所以我猜它发生得太早了。

So in the new window code, at the bottom of the page I added

所以在新窗口代码中,我在页面底部添加了

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

It does not feel like solid practice but if you need it to work... The 100mSec seems to be the lowest that works on my system.

这感觉不像是可靠的练习,但如果你需要它来工作...... 100mSec 似乎是在我的系统上工作的最低值。

回答by Manish Nagar

yes you can do this by this code you have give onBlur="self.focus()" in body for child window

是的,您可以通过此代码执行此操作,您在子窗口的正文中提供了 onBlur="self.focus()"

    //Parent page...
    <html>
      <body>
      <a href="#" onClick="window.open('two.html','sdvwsv','width=200,height=200');">here...</a>
         </body>
     </html>


   //Child page...
         <html>
          <body onBlur="self.focus();">
               here...
              </body>
          </html>

回答by Hemant

<html>
    <script language="JavaScript">
    <!--
    function openWin(){
      var myBars = 'directories=no,location=no,menubar=no,status=no';

      myBars += ',titlebar=no,toolbar=no';
      var myOptions = 'scrollbars=no,width=400,height=200,resizeable=no,top=10, left=10,';
      var myFeatures = myBars + ',' + myOptions;
      var myReadme = 'This is a test.'

      var newWin = open('', 'myDoc', myFeatures);

      newWin.document.writeln('<form>');
      newWin.document.writeln('<table>');
      newWin.document.writeln('<tr valign=TOP><td>');
      newWin.document.writeln('<textarea cols=45 rows=7 wrap=SOFT>');
      newWin.document.writeln(myReadme + '</textarea>');
      newWin.document.writeln('</td></tr>');
      newWin.document.writeln('<tr><td>');
      newWin.document.writeln('<input type=BUTTON value="Close"');
      newWin.document.writeln(' onClick="window.close()">');
      newWin.document.writeln('</td></tr>');
      newWin.document.writeln('</table></form>');
      newWin.document.close();
      newWin.focus();
    }
    -->
    </script>
    <body>
    <form>
      <b>Click the following button to open a new window: </b>
      <input type=BUTTON value="Open" onClick='openWin()'>
    </form>
    </body>

回答by Hemant

<html>
    <script language="JavaScript">
    <!--
    function openWin(){
      var myBars = 'directories=no,location=no,menubar=no,status=no';
      myBars += ',titlebar=no,toolbar=no';
      var myOptions = 'scrollbars=no,width=600,height=400,resizeable=no,top=10, left=10';
      var myFeatures = myBars + ',' + myOptions;
      var newWin = open('test.html', '', myFeatures);
      newWin.document.close();
      newWin.focus();
    }
    -->
    </script>
    <body>
    <form>
      <b>Click the following button to open a new window: </b>
      <input type=BUTTON value="Open" onClick='openWin()'>
    </form>
    </body>
</html>
    </html>