How to call Javascript function of parent window from new pop up window

Context:

When we use window.open to display pop up at some instance we wish to call a function of the parent window or let the parent window know about some task of pop up is done now and parent should close it.

For that only defining a java script function on parent page is not sufficient.

Implementation:

Suppose you want to open URL htttp://example.com/page_popup.html  in pop up from http://example.com/page.html in new window.  after pop up displayed and some event or task is over you need to let parent page ( http://example.com/page.html ) know the task is over and you can process further(let say make some ajax request after pop up finishes it’s task).

I have typed the code  and not tested, it is just to describe the Idea. So if  it doesn’t work  or have any queries Please let me know.

in page.html

</pre>
<head>

<script >

// assigning new property with function so child window can point it

document.functionToBeCalledFromChildWindow = function (param){

alert(param);

// further processing.

}

</script>

</head>

<body>

<button onClick="window.open('<strong>htttp://example.com/page_popup.html</strong>')">Open window</button>

</body>

in page_popup.html

<head>

<script >

function onload()

{

// some code for task

alert('task completed');

window.opener.document.functionToBeCalledFromChildWindow('my task is over you can proceed');

window.close();

}

</script>

</head>

<body>

Hi I am Pop Up and Able let my parent window know I am done with my task.

</body>
<pre>
Advertisement

2 comments on “How to call Javascript function of parent window from new pop up window

  1. […] How to call Javascript function of parent window from new … – May 15, 2012  · Context: When we use window.open to display pop up at some instance we wish to call a function of the parent window or let the parent window … […]

  2. Joao Junior says:

    thank you.. this definitely helped me to move on with a similar script i am encoding 😀

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s