The following statement is used because the method in question (refreshPartyList()) may not always be defined.
try {
parent.document.getElementById("myId").contentWindow.refreshPartyList(param1, param2);
}catch(e){}
Currently using the above approach and it is working fine, but is there a better alternative?
The following statement is used because the method in question (refreshPartyList()) may not always be defined.
try {
parent.document.getElementById("myId").contentWindow.refreshPartyList(param1, param2);
}catch(e){}
Currently using the above approach and it is working fine, but is there a better alternative?
Share Improve this question asked Nov 1, 2011 at 1:06 SnowrightSnowright 6254 gold badges15 silver badges22 bronze badges3 Answers
Reset to default 4var e = parent.document.getElementById("myId").contentWindow;
if(e.refreshPartyList)
e.refreshPartyList(param1, param2);
You can check if the method exists with a simple if:
if (parent.document.getElementById("myId").contentWindow.refreshPartyList) {
parent.document.getElementById("myId").contentWindow.refreshPartyList(param1, param2);
}
Or better still, with jQuery (because the if
does not guarantee it is a function):
if (jQuery.type(parent.document.getElementById("myId").contentWindow.refreshPartyList) == 'function') {
var win = parent.document.getElementById("myId").contentWindow;
win.refreshPartyList && win.refreshPartyList(param1, param2);
Use x.method && x.method(...)
to check whether the method exists.