2008/04/16

javascript : How to call opener function & set opener element ?

Opener window : openerwindow.html



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
<title>Opener window</title>
</head>
<script type="text/javascript" language="javascript">
function OpenNewWindow()
{
window.open("newwindow.html");
}
function CallMe()
{
alert("hello!");
}
</script>
<body>
<form action="#" method="post" name="form1">
<a id="clickme" href="#" onclick="OpenNewWindow();">Click me !</a>
<br />
<input name="textbox1" type="text" value="" />
</form>
</body>
</html>



New window : newwindow.html



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
<title>New window</title>
</head>
<script type="text/javascript" language="javascript">
function CallOpenerFunction()
{
if (!window.opener.closed)
{
// call opener function
window.opener.CallMe();
}
}
function SetOpenerElement()
{
if (!window.opener.closed)
{
// set opener element
var tf = opener.document.forms[0];
tf.textbox1.value = "hi!";
}
}
</script>
<body>
<a href="#" onclick="CallOpenerFunction();">call opener function</a>
<br />
<a href="#" onclick="SetOpenerElement();">set opener element</a>
</body>
</html>

90% 症候群