【问题标题】:Keep focused on pop up window until closed一直专注于弹出窗口直到关闭
【发布时间】:2017-03-14 08:46:13
【问题描述】:

我想将焦点保持在新的弹出窗口中,并避免用户单击父窗口,除非弹出窗口关闭。每次单击链接时,我下面的代码都将重点放在弹出窗口上。我想在每次打开时都将焦点放在弹出窗口上。

下面是我的html代码。

function PopupCenter(url, title, w, h) {
  var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
  var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
  var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
  var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;

  var left = ((width / 2) - (w / 2)) + dualScreenLeft;
  var top = ((height / 2) - (h / 2)) + dualScreenTop;
  var newWindow = window.open(url, title, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);

  // Puts focus on the newWindow
  if (window.focus) {
    newWindow.focus();
  }
}
<a href="" class="btn btn-outline btn-info btn-xs" onClick="PopupCenter('transfer.php?id=<?php echo $putIn;?>','xtf','980','350');">Transfer</a>

【问题讨论】:

    标签: javascript html popup


    【解决方案1】:

    您可以通过以下方式实现focus

    function popitup(url) {
      newwindow = window.open(url, 'name', 'height=200,width=150');
      if (window.focus) {
        newwindow.focus()
      }
    
      if (!newwindow.closed) {
        newwindow.focus()
      }
      return false;
    }
    

    编辑: 如果上述方法不起作用,那么另一种方法是从父窗口中移除焦点

    <html>
    <head>
    <script type="text/javascript">
    
    var popupWindow=null;
    
    function child_open()
    { 
    
    popupWindow =window.open('new.jsp',"_blank","directories=no, status=no, menubar=no, scrollbars=yes, resizable=no,width=600, height=280,top=200,left=200");
    
    }
    function parent_disable() {
    if(popupWindow && !popupWindow.closed)
    popupWindow.focus();
    }
    </script>
    </head>
    <body onFocus="parent_disable();" onclick="parent_disable();">
        <a href="javascript:child_open()">Click me</a>
    </body>    
    </html>
    

    【讨论】:

    • 嗨。它没有用。当我点击父窗口时它没有保持焦点。
    • 嗨@shubham agrawal。每次点击链接时它都会打开新的弹出窗口。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多