简体   繁体   English

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

[英]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.

相关问题 使用javascript将值从子级传递到父级窗口 - using javascript to pass value from a child to parent window 如何在Javascript中使用window.open将值从子窗口返回到父窗口? - How to return value from child window to parent window using window.open in Javascript? 如何使用Javascript和/或JSP将数据从子窗口传递到父窗口 - How to pass data from a child window to a parent window using Javascript and/or JSP javascript-将值从父窗口传递到子窗口以及函数中 - javascript - pass a value from parent to child window and within a function 如何使用javascript从父窗口获取子窗口URL - How to get child window url from parent window using javascript 如何使用JavaScript将单选按钮的选定值从弹出窗口传递到父窗口? - How to pass the selected value of radio button using JavaScript from popup window to parent window? 从子窗口将URL中的某些值传递到父窗口 - pass some value in the url to the parent window from child window 如何使用window.open将参数从父窗口传递到子窗口 - How to pass a parameter to the child window from parent window using window.open 使用JavaScript从具有多个父窗口的子窗口刷新父窗口 - Refresh parent window from child with multiple parent window using javascript 如何在父窗口和子窗口之间传递值 - How to pass value between parent window and child window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM