我有一个JavaScript函数"print_something",它在大约300个jsp帮助页面中实现。我发现这个"print_something“函数必须修正。所以我正在寻找一个不改变300个文件的解决方案。
我有一个打开自定义帮助页面的页面:
window.open('SomeHelpPage101.htm', 'Help', 'location=no,status=no,height=500,width=600,resizable,scrollbars'); 我试着像这样重新定义函数:
var jsObject = window.open('SomeHelpPage101.htm', 'Help', 'location=no,status=no,height=500,width=600,resizable,scrollbars');
jsObject.print_something() = function(){//corrected function}如果我一步一步地做,它在Firebug中工作得很好。但是如果我运行代码,就会发现window.open(...)还没有完成,因为它是异步的,所以我的重定义不起作用。
如何强制window.open(...)如果先完成,然后再重新定义print_something(),这将是成功的。
非常提前感谢您。
发布于 2012-01-24 01:47:32
我想这个已经被介绍过了,所以这里有一个链接:
发布于 2012-01-31 16:50:16
非常感谢你们的帮助和建议。:)
解决方案是使用setInterval函数,我们在该函数中等待并检查对象是否已初始化。
function Help()
{
// asynchronous object initialisation with "window.open"
var jsOBJ = window.open('HelpPage' + pageID + '.htm', 'Help', 'location=no,status=no,height=500,width=600,resizable,scrollbars');
// check every 100 ms if "window.open" has finished and "jsOBJ.custom_print" has initialised
var helpTimer = setInterval(function() {
if(jsOBJ.custom_print) {
clearInterval(helpTimer);
// override function which is declared in child pages which we open with "window.open"
jsOBJ.custom_print = function() {
alert('Test call from redefined parent function.');
jsOBJ.print();
};
}
}, 100);
}https://stackoverflow.com/questions/8976099
复制相似问题