René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

JavaScript: Accessing variables in the opening window

The following example demonstrates how an opened window can set a variable in its opening window.

opener_.html

opener_.html defines a global variables named just_a_variable and sets it to 42. There are two buttons, one of which opens a small window (opened_.html) while the other button shows the actual value of just_a_variable.
<html>
<head>
<title>Opening window</title>
<script language='javascript'>

var just_a_variable=42;

function open_window() {
  window.open('opened_.html', '', 'toolbar=no,scrollbars=yes,width=200,height=200,top=300,left=100');
}

</script>
</head>
<body>

  <form>
    <input type='button' value='open a window' onClick='javascript:open_window()'>
    <input type='button' value='show var' onClick='javascript:t.value=just_a_variable'>
    <input type='text' name='t'>
  </form>

</body>
</html>

opened_.html

opened_.html gets opened by clicking on of the buttons of opener_.html. Through the special variable opener it is possible for the opened window to access the window object in the opening window, which can be used to alter the value of just_a_variable. By clicking the button, this will be done.
<html>
<head>

<title>Opened window</title>

<script language='javascript'>

  function set_var_in_opener(v) {
    opener.window.just_a_variable=v;
  }

</script>

</head>
<body>

  <form>
    <b>Variable:</b>
    <input type='text' name='variable'>
    <br><input type='button' onClick='javascript:set_var_in_opener(variable.value)' value='set variable in opener window'>
  </form>

</body>
</html>