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

Get Local Time by Address using Earth tools and Google maps(Location To Time PHP)

Context:

Once I was needed to fetch correct time of the location by it’s address. My project was a portal application and the user should be facilitated with the local time of other users. so I have made a function that can calculate correct time  of the address supplied.

Implementation:

This function uses two APIS

1) Google maps

2) Earth tools

By calling Google maps I have fetched lat & lng of the location and passed them to earth tools.

Earth tool  provides the correct time at the lat&lng supplied.

/*@param $address (string) comma separated address (full or partial)

*/

public function GetLocalTime($address)
{
// fetching lat&amp;lng from Google Maps
$request_uri = 'http://maps.googleapis.com/maps/api/geocode/xml?address='.urlencode($address).'&amp;sensor=true';
$google_xml = simplexml_load_file($request_uri);
$lat = (string)$google_xml-&gt;result-&gt;geometry-&gt;location-&gt;lat;
// fetching time from earth tools
$lng = (string)$google_xml-&gt;result-&gt;geometry-&gt;location-&gt;lng;
$earth_uri  = "http://www.earthtools.org/timezone/$lat/$lng";
$earth_response = simplexml_load_file($earth_uri);
return (string)$earth_response-&gt;localtime;
}

echo GetLocalTime('Ahmedabad,Gujarat');

Let me know if it is useful to you or have any query, Thanks for reading.

Regards,

Rupesh Patel