JavaScript, Web Development

JavaScript – Call Function from Parent Window

Another handy tip for JavaScript : Calling a function from Parent Window.

In a web application, there are times that when in a certain window, you’ve clicked a button. That button opens a new window.

Now that you are in a new window, there comes a time that you need to call a certain function from the parent window. Or you may want to change some form values.

In JavaScript, to open a new window:

window.open(url, windowName[, windowFeatures]);

Assuming you have this function from parent window:

function sampleFunction()
{
   //do some stuff
   alert("I did it!");
}

In your new window, we call it child window, you can call that function using the window.opener object.

window.opener.sampleFunction();

Or even access form values like:

alert(window.opener.formName.txtName.value);
//this is in the child window

Of course it has been discussed thousand times before. Here is where i get it:

http://www.webmasterworld.com/forum91/2957.htm

I posted it here today since I needed that very code in my project. And since I’m happy with the results, I share it with you. 🙂

Leave a reply

Your email address will not be published. Required fields are marked *