How to pass a parameter to the child window from parent window in Java Script by using window.open.
Please give me any idea.
How to pass a parameter to the child window from parent window in Java Script by using window.open.
Please give me any idea.
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.
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
});
When you creating html for child window make a hidden field in html and get in child window.
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'
Pass the value as query string
try folowing
window.open("url.php?param=value¶m2=value2");