【问题标题】:Passing Variable from child popup window to parent popup window将变量从子弹出窗口传递到父弹出窗口
【发布时间】:2015-06-15 04:40:29
【问题描述】:

我有弹出窗口,它转到 upload.jsp,将文件上传到目录。

上传逻辑写在upload.jsp中。我的问题是我想获得父弹出窗口文本字段的保存路径。

【问题讨论】:

标签: javascript jsp jsp-tags


【解决方案1】:

子窗口有一个属性opener,它指的是打开它的窗口。如果他们都来自同一个来源,孩子可以像这样访问父母的全局变量:

opener.globalVariable

这意味着它可以以opener.document 访问父窗口的文档,因此可以使用opener.document.getElementById 或opener.document.querySelector 来获取父窗口中的元素。

示例:

父页面:

<!doctype html>
<html lang="en">
<body>
<input type="text"><input type="button" value="Click me">
<script>
document.querySelector("input[type=text]").value = Math.floor(Math.random() * 10000);
document.querySelector("input[type=button]").addEventListener(
  "click",
  function() {
    var wnd = window.open("popup.html");
  },
  false
);
</script>
</body>
</html>

弹出页面:

<!doctype html>
<html>
<body>
<script>
var field;
if (!opener) {
    display("Not opened as a popup");
} else {
    field = opener.document.querySelector("input[type=text]");
    display("Value is " + field.value);
}

function display(msg) {
    var p = document.createElement('p');
    p.innerHTML = msg;
    document.body.appendChild(p);
}
</script>
</body>
</html>

【讨论】:

  • 谢谢它帮助了我 T.J.克劳德
  • @ManjunathVibhooti:很高兴有帮助。
  • @ManjunathVibhooti:如果这回答了您的问题,按照 SO 的工作方式,您可以通过单击旁边的复选标记“接受”该答案。 More here. 但前提是它回答了您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-03
相关资源
最近更新 更多