1

I have a JavaScript function "print_something", which is implemented in around 300 jsp help pages. I turned out this "print_something" function has to be corrected. So I am searching for a solution not to change 300 files.

I have one page where I open custom help page:

window.open('SomeHelpPage101.htm', 'Help', 'location=no,status=no,height=500,width=600,resizable,scrollbars'); 

I tryed to redefine function like this:

var jsObject = window.open('SomeHelpPage101.htm', 'Help', 'location=no,status=no,height=500,width=600,resizable,scrollbars'); 
jsObject.print_something() = function(){//corrected function}

It all works well in Firebug if I do step by step. But if I run the code it happens that window.open(...) is not yet finished because it is asynchronous so my redefine doesn't work.

How can I force window.open(...) to finish first and afterwards redefining print_something() would be sucessful.

Thank you very much in advance.

Joey Adams
  • 41,996
  • 18
  • 86
  • 115
MerkoSMS
  • 43
  • 1
  • 10
  • `jsObject.print_something() =` does not seem to be right. [According to MDN](https://developer.mozilla.org/en/DOM/window.open), `window.open` will *always* return a reference to the new window if everything worked well. – Felix Kling Jan 23 '12 at 17:43
  • 2
    Rather than putting the same js function in 300 files, you should move that to a standalone `.js` file and then simply include that file on 300 pages. – Alex Wayne Jan 23 '12 at 17:46
  • I don't know if you can do that, but if you can it would be `jsObject.print_something = function () {}`. The `jsObject.print_something()` you have is a method call. – JMM Jan 23 '12 at 18:01
  • http://en.wikipedia.org/wiki/Don't_repeat_yourself. Copypasting same code multiple times == Maintenance trouble. – BalusC Jan 23 '12 at 18:29

2 Answers2

0

I think this has already been covered so here is a link to it:

Set a callback function to a new window in javascript

Community
  • 1
  • 1
crv
  • 3,024
  • 4
  • 27
  • 31
  • I tryed like this: var RunCallbackFunction = function() { }; //reference holder only function custom_print() { alert('parent function!'); self.focus(); self.print(); } var helpPopup = window.open(CustomPage101.htm'); helpPopup.onload = function() { helpPopup.RunCallbackFunction = custom_print; }; But when I try to print parent function is still not allerted. Am I doing something wrong? – MerkoSMS Jan 23 '12 at 18:22
  • seems you missing a ' in var helpPopup = window.open(CustomPage101.htm') do some kind check like (window.onload != null) first so you don't overwrite it. I never tried this, but maybe save the reference in a variable first, then use a wrapper function contain old onload and your callback function instead. – user227353 Jan 23 '12 at 18:22
  • I also tryied: var helpPopup = window.open(CustomPage101.htm'); helpPopup.onload = function() { helpPopup.custom_print = function() { alert('parent function!'); helpPopup.focus(); helpPopup.print(); }; But still not sucess... – MerkoSMS Jan 23 '12 at 18:23
  • check if helpPopup.custom_print was called before document ready. also fix that ' in your code. Also, what you want overwrite is the print function isn't it? or is that the custom_print is what you want to overwrite? – user227353 Jan 23 '12 at 18:45
  • Yes custom_print is the function implemented in all pages I want to redefine. – MerkoSMS Jan 31 '12 at 08:49
0

Thank you guys very much for help and advices. :)

The solution was with setInterval function where we wait and check if object is initialised.

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);   
}
MerkoSMS
  • 43
  • 1
  • 10