[英]How to pass a parameter to the child window from parent window using window.open
[英]How to return value from child window to parent window using window.open in Javascript?
我有一個父窗口,我使用 window.open 打開子窗口,如下所示。 我想從子窗口獲取一個布爾值,在此基礎上我將在父窗口中執行我的任務。
function openChildWin()
{
var childWin = window.open("childWin.html", "_blank", "height=400, width=550, status=yes, toolbar=no, menubar=no, location=no,addressbar=no);
if (childWin)
{
//some logic
}
else
{
//some logic
}
}
**childWin.html**
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript">
function OKClicked()
{
//return true; //I want to return true here
window.close(); //Close this child window
}
</script>
</head>
<body>
<button type="button" id="OKButton" onclick="OKClicked()">Click ME</button>
</body>
</html>
單擊按鈕時,我想從這里返回 true。 但這一切對我都不起作用。 我在這里缺少語法嗎?
你可以這樣做:
在父級:
function openChildWin() {
var childWin = window.open("childWin.html", "_blank", "height=400, width=550, status=yes, toolbar=no, menubar=no, location=no,addressbar=no");
}
function setValue(val1) {
// do your logic here
}
在彈出窗口中:
function OKClicked() {
window.opener.setValue(true);
}
使用下面的代碼並將 test 替換為您的元素 ID。
var parentDoc = window.opener.document;
parentDoc.getElementById("test").value = document.getElementById("test").value;
您可以使用window.opener.postMessage()
:
在父母中:
window.onmessage = function(m) {
if(m.data) {
// logic if 'OK' in child clicked
}
}
在孩子:
function OKClicked() {
window.opener.postMessage(true, '*');
window.close();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.