【问题标题】:How (Javascript) return focus to the OPENER window (without changing its URL) - from its (child) popup?如何(Javascript)从它的(子)弹出窗口将焦点返回到 OPENER 窗口(不更改其 URL)?
【发布时间】:2020-11-13 03:11:46
【问题描述】:

我有一个页面(我将其称为“打开器”窗口),其中包含用于打开“弹出”窗口(在同一域中)的链接 -

var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
winPopupObjRef = window.open("popupPage.htm","popupWindowName",'width=300,height=200,resizable,scrollbars,status,' width='+w+', height='+h+', top='+top+', left='+left);");

我们需要允许访问者返回到“打开器”窗口。他们当然可以轻松地手动执行此操作,因为弹出窗口很小且位于中心,周围有(较大的)“开启器”窗口。 即,如果访问者在“打开器”区域单击 - “弹出窗口”消失,他们可以继续浏览网站。

如何以编程方式执行访问者可以手动执行的操作? {PS 不关闭弹窗}

我已经搜索并尝试过(不成功;(

  • window.opener()
  • window.blur()

我知道窗口的“名称”—— 有没有办法通过'name'打开window.open而不说明url?

{我假设我无法说明 URL - 因为访问者可能:

  1. 点击打开弹窗,
  2. 返回开瓶器继续 浏览
  3. 再次查看弹出窗口
  4. 在上面第 2 和第 3 圈圈

认为:除非有办法在每次转圈时捕获当前 url?

我用来打开弹出窗口的代码如下所示(参见:https://developer.mozilla.org/en-US/docs/Web/API/Window/open),我想我也可以在弹出窗口中修改它 - 链接回“开启器”。任何帮助都非常感谢。

<script type="text/javascript">
var windowObjectReference = null; // global variablemanually 

function openFFPromotionPopup() {
  if(windowObjectReference == null || windowObjectReference.closed)
  /* if the pointer to the window object in memory does not exist
     or if such pointer exists but the window was closed */

  {
    windowObjectReference = window.open("http://www.spreadfirefox.com/",
   "PromoteFirefoxWindowName", "resizable,scrollbars,status");
    /* then create it. The new window will be created and
       will be brought on top of any other window. */
  }
  else
  {
    windowObjectReference.focus();
    /* else the window reference must exist and the window
       is not closed; therefore, we can bring it back on top of any other
       window with the focus() method. There would be no need to re-create
       the window or to reload the referenced resource. */
  };
}
</script>

(...)

<p><a
 href="http://www.spreadfirefox.com/"
 target="PromoteFirefoxWindowName"
 onclick="openFFPromotionPopup(); return false;" 
 title="This link will create a new window or will re-use an already opened one"
>Promote Firefox adoption</a></p>

长期目标是帮助移动设备 - 弹出窗口后面可能看不到“开启器”

============================在上面画一条线============== =======

我认为我使用(上面)来自“Mozilla 最佳实践”的代码误导了您。 下面是我试图开始工作的代码。

这里是“打开器”窗口的代码:“/Calling_mainPage.htm”

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Opening/Main page</title>

<script type="text/javascript">

//var windowParentObjectReference = window.open("Calling_mainPage.htm","OpenerWinName"); // Name the opening page, in case I find we can return to it by name
//console.log({windowParentObjectReference});

// Modified extract from Mozilla Best Practises
var windowPopupObjectReference = null; // global variable

function openPopupWin() {

//alert ({ windowPopupObjectReference});
//console.log(" hello world ");

  if(windowPopupObjectReference== null || windowPopupObjectReference.closed)
  /* if the pointer to the window object in memory does not exist
     or if such pointer exists but the window was closed */

  {
    windowPopupObjectReference = window.open('popupPage.htm','popupWindowName','width=300,height=200,left=200,top=200');
    /* then create it. The new window will be created and
       will be brought on top of any other window. */
  }
  else
  {
//  alert (windowPopupObjectReference);
    windowPopupObjectReference.focus();
    /* else the window reference must exist and the window
       is not closed; therefore, we can bring it back on top of any other
       window with the focus() method. There would be no need to re-create
       the window or to reload the referenced resource. */
  };
}
</script>
</head>

<body>

Opening/Main page

<p><a
 href="#"
 target="PopupWindowName"
 onclick="openPopupWin(); return false;"
 title="This link will (hopefully) create a new window or will re-use an already opened one">Open/Create the popup Window</a></p>
</body>
</html>

这里是弹出窗口的代码:“/popupPage.htm”

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>popup page</title>

<script type="text/javascript">
// Modelled on Mozilla Best Practises

// var windowParentObjectReference = true; // set global variable
// console.log({windowParentObjectReference});

function openParentWin() {
  if(windowParentObjectReference == null || windowParentObjectReference.closed)
  /* if the pointer to the window object in memory does not exist
     or if such pointer exists but the window was closed */

  {
//    alert("Hello! new win needed!");
    windowParentObjectReference = window.open("Calling_mainPage.htm","ParentWindowName","resizable,scrollbars,status");
    /* then create it. The new window will be created and
       will be brought on top of any other window. */
  }
  else
  {
 //   alert("focus on existing win!");
    windowParentObjectReference.focus();
    /* else the window reference must exist and the window
       is not closed; therefore, we can bring it back on top of any other
       window with the focus() method. There would be no need to re-create
       the window or to reload the referenced resource. */
  };
}
</script>
</head>
<body style="background:grey;">
popup page
<p><a
 href="#"
 target="ParentWindowName"
 onclick="openParentWin();//return false;"
 title="This link will create a new window or will re-use an already opened one">Return (hopefully) to the Parent/Opener WIndow</a></p>
</body>
</html>

Opener Window 调用 Popup 窗口 OK, 但是 Popup 不会返回到 Opener。

【问题讨论】:

    标签: javascript windows cross-browser


    【解决方案1】:

    一旦新窗口包含来自另一个来源的页面,所有赌注都将被取消。您可以尝试使用同源页面在新窗口中嵌入 iFrame,然后您可以继续操作 opener。

    您不能查询 URL 或包含外部页面 URL 的 iframe

    这是一个改进。它在像 SO 这样的沙盒环境中根本不起作用

    let windowObjectReference = null; // global variablemanually 
    
    const pop = function(e) {
      if (windowObjectReference == null || windowObjectReference.closed)  {
        windowObjectReference = window.open("http://www.spreadfirefox.com/",
       "PromoteFirefoxWindowName", "resizable,scrollbars,status");
       if (windowObjectReference) e.preventDefault(); // allow the target to work in case of popup blockers
      }
      else {
        windowObjectReference.focus();
      }
    }
    
    "click touchstart".split(" ").forEach(function(e){
      document.querySelector("[target=PromoteFirefoxWindowName]").addEventListener(e,pop);
    });
    <p><a
     href="http://www.spreadfirefox.com/"
     target="PromoteFirefoxWindowName"
      title="This link will create a new window or will re-use an already opened one"
    >Promote Firefox adoption</a></p>

    好的,这里是如何允许父窗口操作弹出窗口

    1. 创建此页面

    它必须与嵌入的 iframe 内容具有相同的协议,所以我建议如果您拥有 spreadfirefox.com,则将其设为 https

    <!DOCTYPE html>
    <html>
    <head>
      <style>
        .container {
          position: relative;
        }
        .container iframe {
          position: absolute;
          top: 0;
          left: 0px;
          width: 100%;
          height: 100vh;
          padding: 0px;
          margin: 0px;
        }
      </style>
    </head>
    <body>
      <div class="container">
        <iframe scrolling="no" src="https://www.spreadfirefox.com/" frameBorder="0"></iframe>
      </div>
    </body>
    </html>
    
    1. 现在您可以在带有 iframe 的页面中添加模糊脚本,并使用窗口句柄重新调整弹出窗口的焦点,因为弹出窗口现在与您的脚本和父级来自同一服务器

    【讨论】:

    • 您说“来自另一个来源”是您使用“起源”这个词=“域”吗?如果不同,请解释一下。
    • 非常感谢您添加“...允许目标在弹出窗口阻止程序的情况下工作”。 }
    • 来源是域协议和端口
    • 谢谢了解。我的两个页面都是相同的来源 - 所以希望“再次下注”?
    • 是的,所以我的问题是,剩下的问题是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多