Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a callback function to a new window in javascript

Is there an easy way to set a "callback" function to a new window that is opened in javascript? I'd like to run a function of the parent from the new window, but I want the parent to be able to set the name of this particular function (so it shouldn't be hardcoded in the new windows page).

For example in the parent I have:

function DoSomething { alert('Something'); } ... <input type="button" onClick="OpenNewWindow(linktonewwindow,DoSomething);" /> 

And in the child window I want to:

<input type="button" onClick="RunCallbackFunction();" /> 

The question is how to create this OpenNewWindow and RunCallbackFunction functions. I though about sending the function's name as a query parameter to the new window (where the server side script generates the appropriate function calls in the generated child's HTML), which works, but I was thinking whether there is another, or better way to accomplish this, maybe something that doesn't even require server side tinkering.

Pure javascript, server side solutions and jQuery (or other frameworks) are all welcomed.

like image 790
SztupY Avatar asked May 09 '10 12:05

SztupY


People also ask

How do I create a new window in JavaScript?

You can use JavaScript to launch a new window. The window. open() method, which allows you to open up new browser window without navigating away from the current page. It is useful when you need to display some popup advertisement or the instructions without navigating away from the current window.

How do you add a callback to a function?

A custom callback function can be created by using the callback keyword as the last parameter. It can then be invoked by calling the callback() function at the end of the function. The typeof operator is optionally used to check if the argument passed is actually a function. console.

How does callback function work in JavaScript?

A JavaScript callback is a function which is to be executed after another function has finished execution. A more formal definition would be - Any function that is passed as an argument to another function so that it can be executed in that other function is called as a callback function.

Can a callback function call another function?

Yes. The print( ) function takes another function as a parameter and calls it inside. This is valid in JavaScript and we call it a “callback”. So a function that is passed to another function as a parameter is a callback function.


1 Answers

Updated for comments: If you're opening your window via window.open() then in your child page you can set a function in the child to just be a reference pointing to a parent function, so have this in the child page:

var RunCallbackFunction = function() { }; //reference holder only 

Then in your parent (opener), set that function when that child window loads, like this:

//random function you want to call function myFunc() { alert("I'm a function in the parent window"); }  //to actually open the window.. var win = window.open("window.html"); win.onload = function() { win.RunCallbackFunction = myFunc; }; 

This assigns the function in your parent to now be the target of that child...and you can point each child to a different function if you wish, they're all independent.

like image 113
Nick Craver Avatar answered Sep 28 '22 04:09

Nick Craver