1

How to pass a parameter to the child window from parent window in Java Script by using window.open.

Please give me any idea.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Naing Lay
  • 29
  • 1
  • 4
  • possible duplicate of [javascript - pass selected value from popup window to parent window input box](http://stackoverflow.com/questions/9994120/javascript-pass-selected-value-from-popup-window-to-parent-window-input-box) – Ja͢ck Mar 12 '14 at 05:34
  • You could pass it as part of the url (querystring). – Vahe Khachikyan Mar 12 '14 at 05:35
  • I just try like that without pass parameter . But I would like to pass parameter by using window open . How to do . can you give me any idea. popupWindow = window.open(url,'popUpWindow','height=500,width=500,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes'); – Naing Lay Mar 12 '14 at 05:37
  • Please add parameters as querystring with your url example : url="test.php?param1=value1&param2=value2" – Sarath Mar 12 '14 at 05:42

5 Answers5

1

You can use query string to pass parameters, That's a better approach.

Regarding your requirement of passing parameters by using window.open. You can access some elements and global variables but there value cannot be retained. eg: when you execute the following code

popupWindow = window.open(url,'popUpWindow','height=500,width=500'); 
popupWindow.alert("test");

you will see an alert when new window is opened. But the data will load after that alert only. So even you are able to set value of any global variable in child javascript, but the value cannot be retained because page loads after window is opened, so data gets refreshed.

Rajesh Dhiman
  • 1,888
  • 1
  • 17
  • 30
1

I originally tried this:

parent window:

 var parms = {arg1: "arg1", arg2:"arg2"};
 var handle = window.open(url);
 // stringifyed to make sure the child window does 
 //not try to access object by reference from child to parent.
 handle.window.parameters = JSON.stringify(parms);

child window:

$(document).ready(function(){
    var args = JSON.parse( window.parameters);
    // and so on.
});

The problem was that the value was occasionally being set before the child window was ready so when the child window was ready, it would be undefined.

So Instead, I used sessionStorage (note: this will not work cross domain.)

Parent:

// the user can cause more than one child window so I give storage a unique id.
var parms = JSON.stringify({arg1: "arg1", arg2:"arg2"});
var storageId = "parms" + String(Date.now());
sessionStorage.setItem(storageId, parms);
window.open(url + "?sid=" + storageId);

This on the child:

$(document).ready(function(){
    var prmstr = window.location.search.split("=");
    var sid = prmstr[1];
    var args = JSON.parse(sessionStorage.getItem(sid));
    sessionStorage.removeItem(sid);
    // and so on
});
Bindrid
  • 3,655
  • 2
  • 17
  • 18
0

When you creating html for child window make a hidden field in html and get in child window.

Harshit Tailor
  • 3,261
  • 6
  • 27
  • 40
0

What kind of parameter? I mean, what's the parameter for? For example, you could add a GET value in the url:

window.open("http://example.com/script.php?KEY=value")

And then you would have in your (in PHP) $_GET['KEY'] that 'value'

0

Pass the value as query string

try folowing

window.open("url.php?param=value&param2=value2");
Sarath
  • 608
  • 4
  • 12