Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Communicating between two windows --

Sending data between pages

Communicating between two windows --

by  link9  Posted    (Edited  )
Crash course here -- this question has been asked a few times in the last couple of days, so I worked up this very simple example that I think lays the groundwork for what could be turned into something much more complicated if you're so inclined.

The question is, "Can two browser windows communicate between each other?"

The answer is yes, and here's how:

If you have a parent-child relationship between the two browsers, it's fairly simple. Here's an example:

thePage.htm:
<html>
<head>
<title>Test</title>
<script language="javascript">
function openMe(){
var x = window.open('newOne.htm', 'newWindow', 'height=300,width=300');
}
</script>
</head>
<body>
<form name=theForm>
<INPUT type=button value="Open It" name=submit1 onClick="openMe();">
<input type=text name=theBox>
</form>
</body>
</html>


newOne.htm:
<html>
<head>
<title>testing</title>
<script language=javascript>
function setOther(){
window.opener.theForm.theBox.value = document.myForm.thisBox.value;
}
</script>
</head>
<body>
<form name=myForm>
<input type=text name=thisBox>
<input type=button value="Click Me" onClick="setOther();">
</form>
</body>
</html>


Now all that does is when you click the button, it opens up the new page in a pop up window, and when you type something in the text box in the pop up, and push that button, it assigns that value back to the text box on the original page. From this example, though, it's easy to see how to communicate between the two windows.

Happy Coding Everyone! :)
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top