Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript回调_Javascript_Jquery_Html_Jsp_Callback - Fatal编程技术网

Javascript回调

Javascript回调,javascript,jquery,html,jsp,callback,Javascript,Jquery,Html,Jsp,Callback,我有一个JavaScript函数“print_something”,它在大约300个jsp帮助页面中实现。 我发现这个“print_something”函数必须被更正。 因此,我正在寻找一个解决方案,不改变300个文件 我有一个打开自定义帮助页面的页面: window.open('SomeHelpPage101.htm', 'Help', 'location=no,status=no,height=500,width=600,resizable,scrollbars'); 我试图像这样重新定

我有一个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()将是成功的


提前非常感谢。

我想这已经包括在内了,所以这里有一个链接:


非常感谢你们的帮助和建议。:)

解决方案是使用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);   
}

jsObject.print\u something()=
似乎不对
window。如果一切正常,open
将始终返回对新窗口的引用。与其将相同的js函数放入300个文件中,不如将其移动到一个独立的
.js
文件中,然后将该文件包含在300页中。我不知道您是否可以这样做,但如果可以,它将是
jsObject.print\u something=function(){}
。您拥有的
jsObject.print\u something()
是一个方法调用。。复制粘贴同一代码多次==维护问题。我尝试如下:var RunCallbackFunction=function(){}//仅引用持有者函数custom_print(){alert('parent function!');self.focus();self.print();}var helpPopup=window.open(CustomPage101.htm');helpPopup.onload=function(){helpPopup.RunCallbackFunction=custom_print;};但当我试图打印时,仍然没有调用父函数。我做错什么了吗?似乎您缺少一个“在var helpPopup=window.open(CustomPage101.htm)”中,请先执行一些类似(window.onload!=null)的检查,以免覆盖它。我从未尝试过这样做,但可能会先将引用保存在变量中,然后使用包含旧onload的包装函数和回调函数;helpPopup.onload=function(){helpPopup.custom_print=function(){alert('parent function!');helpPopup.focus();helpPopup.print();};但仍不成功…检查是否在文档准备就绪之前调用了helpPopup.custom_print。同时在代码中修复该问题。另外,您要覆盖的是打印功能,不是吗?或者自定义_print是您要覆盖的?是的custom_print是我要重新定义的所有页面中实现的功能。