RSS

JavaScript How to Interact with Parent window from child window

30 Nov

javascript

parent.html

<html>
<head>
<script >
// assigning new property with function so child window can point it
document.functionToBeCalledFromChildWindow = function (param){
  alert(param);
}
 
</script>
</head>
<body>
 <div id="response"></div>
<button onClick="window.open('child.html', '_blank', 'width=500,height=500')">Open window</button>
</body>
</html>

child.html

<html>
<head>  
</head>
<body>
 <script >
   window.onload = after;
   function after(evt)
   {
       
   }

//Here we call function "functionToBeCalledFromChildWindow" in parent window.
//window.opener.document.functionToBeCalledFromChildWindow('Mom, U there?');

//Here we access DOM in parent.
 window.opener.document.getElementById("response").innerHTML = "Success";
 window.close();
 opener.window.focus()
</script>

Hi I am the only child here.
 
</body>
</html>
Advertisements
 
Leave a comment

Posted by on November 30, 2014 in JavaScript

 

Tags:

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

 
%d bloggers like this: