如何使用javascript将值从子窗口传递到父窗口

时间:2014-05-08 05:45:13

标签: javascript php jquery

<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>
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;
}

我是javascript的新手。我的要求是,当我点击按钮时,必须popup new child window api call [我已在上面的代码中完成] 但如果用户已经loggedIn,则弹出窗口必须自动关闭并在main[parent] window中显示 json 值。

所以当我点击按钮时,它只是打开子窗口及其空白,没有进一步的变化,如窗口退出或在父窗口中显示内容

感谢。

2 个答案:

答案 0 :(得分:1)

您可以使用此方案执行此操作;

当用户点击按钮时,您将打开一个包含网址http://localhost.xxxxx的窗口。在此URL中,您将检查用户是否登录。 如果用户登录,则回显javascript代码以将json值写入父窗口,否则继续使用所需页面。

父窗口

<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>

子窗口内容(在此子窗口中打开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

答案 1 :(得分:0)

window.opener.document.forms[0].getElementById('Txtparent').value = "Value From Popup Page";

window.opener引用父窗口。

我假设您有一个ID Txtparent 的文本框,您要设置值。

文本元素在表单下。如果您还有其他需要,请详细说明您的问题。

由于