Domain Empire

Howto: Change Variables Between Windows With JavaScript

Spaceship Spaceship
Watch
Impact
1
Have you always wanted to communicate between windows like the parent and child and change variables or other unchangable material? Well today is your lucky day!

Just recently, while designing my latest site, I came up with a way to comunicate whatever you want between windows without using build in javascript functions. Want to know how to do it?

USE THE STATUS BAR

Ever thought of that before?

There are somne security issues and it is not foolproof, but hey - what do you have to lose in these situations?

Here is an example.

Parent window (default browser window) opens up a Child window names (New_Window).

The parent window has a variable called "hello" with the string of "not_set" - The child window wants to change the string in this variable to "set".

This is the code in the child window:

----------Child Window----------
<HTML>
<HEAD>
<TITLE> </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- hide me
function myfunc()
{
top.window.opener.status = "action change var hello value set";
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<button onClick="myfunc()">Click me!</button>
</BODY>
</HTML>
------------------------------
Now you might be saying "What was the good in that?", this is why.

Consider this code in the parent window:
----------Default Window----------
<HTML>
<HEAD>
<TITLE>My Page</TITLE>
</HEAD>
<BODY>
<A HREF="javascript:void(1)" onClick="window.open('secondpage.htm','new_window','resizable')">Open second window</a>


<!-- Here is the part you want to pay attention to: -->
<SCRIPT LANGUAGE="JavaScript">
<!-- hide me
var timeout_1;
var hello;
hello = "not_set";

function repeating_func()
{
// You could make the next part a function if you
// wanted to..

if (top.window.status == "action change var hello value set")
{
hello = "set";
top.window.status = "Done";
}

timeout_1 = setTimeout("repeating_func()",100);
}

repeating_func();
//-->
</SCRIPT>

Now obviously it isn't the best code in the world for security and for speed, but this small start should probably get us further into finding a foolproof way to do these things...

Feel free to post your modifications of the code if you have done so.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back