[英]javascript function name as string
有一个外部库正在发送一些响应,这些响应带有作为字符串函数名称传递的javascript回调。 我希望能够在调用函数回调时执行回调函数:
function LibController(myCallback) {
this.libObject = new ThirdPartyLibrary("onTPLCallback");
}
window.onTPLCallback = function () {
// Call to myCallback() in here
};
function AnotherObject() {
this.myLibController = new LibController(function () {
// Callback stuff here
});
}
// Library object emulation
function ThirdPartyLibrary(callback) {
var sc = document.createElement('script');
sc.innerHTML = 'javascript:' + callback + '();';
document.head.appendChild(sc);
}
可能吗? (jQuery不可用)
您可以尝试将window.onTPLCallback
放置在LibController
构造函数中(它必须在构造ThirtPartyLibrary之前放置):
function LibController(myCallback) {
window.onTPLCallback = function () {
myCallback();
};
this.libObject = new ThirdPartyLibrary("onTPLCallback");
}
function AnotherObject() {
this.myLibController = new LibController(function () {
// Callback stuff here
});
}
// Library object emulation
function ThirdPartyLibrary(callback) {
var sc = document.createElement('script');
sc.innerHTML = 'javascript:' + callback + '();';
document.head.appendChild(sc);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.