【发布时间】: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 - 因为访问者可能:
- 点击打开弹窗,
- 返回开瓶器继续 浏览
- 再次查看弹出窗口
- 在上面第 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