有一个外部库正在发送一些响应,其中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不可用)
答案 0 :(得分:0)
您可以尝试将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);
}