tags:

views:

26

answers:

2

In a popup window, I create a string dynamically which represents a function existing in the window.opener

e.g. funcstring = 'getProductListings("user_id",true)';

This is one of several possible functions that could be created dynamically, so I can't just hardcode it into the window.opener function call.

Then I want to call this in the opener something like as follows, but I'm not sure how to structure it.

window.opener.funcstring; // Pure pseudocode; I have no idea how to construct it.
alert('Success');
window.close();
A: 

you can do this like so:

window[functionName](params)

where functionName === "getProductListings" and params == "user_id",true

You can generally do the above like so:

window.opener[functionName](params);

I hope that helps

Anatoly G
A: 

Well, if the opener window and the opened window are on the same domain (which is an essential access question here), you might be able to call:

window.opener['getProductListings']('user_id', true);

So you would have to modify your funcstring a little, just the method name is required.

Another way to call that is to invoke .call() or .apply() (but that in general is only interesting if you need to change the scope of a function).

window.opener['getProductListings'].apply(<context>, [params]);
jAndy
How would I pass arguments to the opener in this format: getProductListings({"user_id":user_id,"short":true})
Paul
This doesn't seem to work: func = "getProductListings"; params = "{'user_id':1234,'short':true}"; window.opener[func](params);
Paul