andy
andy

Reputation: 53

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

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.

Upvotes: 0

Views: 2007

Answers (2)

H&#252;seyin BABAL
H&#252;seyin BABAL

Reputation: 15550

You can do that with this scenario;

When user clicks button, you will open a window with url http://localhost.xxxxx. In this url, you will check whether user logged in or not. If user logged in, echo a javascript code to write your json value to parent window, else continue with desired page.

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)

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

Upvotes: 1

anirban
anirban

Reputation: 704

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

window.opener referred to parent window.

I assume you have one textbox with id Txtparent, where you want to set values.

And the text element is under a form. If you need anything else, please elaborate your question a bit more detailed way.

Thanks

Upvotes: 0

Related Questions