admin管理员组文章数量:1414940
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.
本文标签: Alternative to trycatch for checking if a function exists in javascriptStack Overflow
版权声明:本文标题:Alternative to trycatch for checking if a function exists in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745193139a2647010.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论