faqts : Computers : Programming : Languages : JavaScript : Windows

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

69 of 107 people (64%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How can I get a Parent Window to call a Daughter windows Javascript function?

Jun 5th, 2000 08:08
Mike Hall, Guy Stables,


The window.open() method returns a handle to the window just opened
and can be used to call functions in that child window using 
the syntax 'window_variable_name.function_name()'.
Here's and example using two html pages:
-- main.html ---------------------------------------------------
<html>
<head>
<title></title>
<script language="JavaScript">
var win = window.open("child.html", "", "width=200,height=200");
function copyChildText() {
  document.forms[0].elements[0].value = win.getText();
}
</script>
</head>
<body>
Parent Window.
<p>
<form>
  <input type="text">
</form>
<p>
<a href="#" onclick="copyChildText(); return false">copy text</a>
</body>
</html>
-- child.html ---------------------------------------------------
<html>
<head>
<title></title>
<script language="JavaScript">
function getText() {
  return document.forms[0].elements[0].value;
}
</script>
</head>
<body>
Child window.
<form>
  <input type="text">
</form>
</body>
</html>
----------------------------------------------------------------
When main.html is loaded, it creates a small pop up window loaded
with child.html and saves the reference to it in the variable 'win'.
child.html contains a javascript function called 'getText()' which
can then be called from the parent window using 'win.getText()'. In
this example, it uses this call to copy the data from the child
window's form to the parent window's form.