[英]how to pass value from child window to parent window using javascript
<html>
<head><title>api gateway</title></head>
<body>
<button class="button" onClick="window.showModalDialog('http://localhost.xxxxx');"><span class="icon">Open</span></button>
</body>
</html>
---php part--- --- PHP部分-
<?php>
session_start();
$_SESSION['user']=1;
if(!empty($_SESSION['user'])) {
$tweets = "entered into condition";
echo '<script>window.opener.document.getElementById("result").value = ' . $tweets . ';window.close();</script>';
exit;
}
I'm new to javascript
. 我是
javascript
新手。 my requirement is when I click on button it must popup
a new child window
with the api call
[which i have done in above code] but if the user is already loggedIn
then the popup window must automatically close and display json value in main[parent] window
. 我的要求是,当我单击按钮时,它必须使用
api call
popup
一个new child window
[我在上面的代码中已经完成],但是如果用户已经loggedIn
则该弹出窗口必须自动关闭并在main[parent] window
显示json值。 main[parent] window
。
so when i click on button it just opens the child window and its blank and no further change like the window exit or displaying the content in parent window 因此,当我单击按钮时,它仅打开子窗口及其空白,并且没有其他更改,例如窗口退出或在父窗口中显示内容
thanks. 谢谢。
You can do that with this scenario; 您可以在这种情况下做到这一点;
When user clicks button, you will open a window with url http://localhost.xxxxx
. 当用户单击按钮时,您将打开一个URL为
http://localhost.xxxxx
的窗口。 In this url, you will check whether user logged in or not. 在此URL中,您将检查用户是否登录。 If user logged in, echo a javascript code to write your json value to parent window, else continue with desired page.
如果用户登录,则回显一个javascript代码以将您的json值写入父窗口,否则继续所需的页面。
Parent Window 父窗口
<html>
<head><title>api gateway</title></head>
<body>
<button class="button" onClick="window.showModalDialog('http://localhost.xxxxx');"><span class="icon">Open</span></button>
<div id="result"></div>
</body>
</html>
Child Window content( http://localhost.xxxxx
opens in this child window) 子窗口内容(在该子窗口中打开
http://localhost.xxxxx
)
<?php
// User logged in
if (!empty($_SESSION["user"])) {
$valueJson = "somevalue"; // Decide your json value
echo '<script>window.opener.document.getElementById("result").value = ' . $valueJson . ';window.close();</script>';
exit;
}
// If user not logged in, go ahead with rendering desired page
window.opener.document.forms[0].getElementById('Txtparent').value = "Value From Popup Page";
window.opener
referred to parent window. window.opener
引用父窗口。
I assume you have one textbox with id Txtparent , where you want to set values. 我假设您有一个ID为Txtparent的文本框,您想在其中设置值。
And the text element is under a form. 文本元素在表单下。 If you need anything else, please elaborate your question a bit more detailed way.
如果您还有其他需要,请详细说明您的问题。
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.