Online Shopping : Computers : Programming : Languages : JavaScript

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

14 of 18 people (78%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

Can I pass a field from one window to another if they are on different servers?

Apr 6th, 2008 20:03
ha mo, Jeremy Lazarus, Colin Fraser, kevin lawlor,


This is possible using a purely client side Javascript solution with a 
couple of caveats. 
First, the URLs for the servers must have the same second level domain 
name. If you have some control over your domain name and (depending on 
the setup of the servers) some control over the servers you will be 
able to achieve what you're looking for.
Second, the windows must have some way of communicating between each 
other. The only way you can achieve this with client side Javascript is 
to open one or both windows using Javascript.
Solution:
1. Set up the two web pages so they have the same second level domain 
name.
For example:
www.mydomainname.com
www2.mydomainname.com
  or
mydomainname.com
other.stuff.mydomainname.com
Both of the examples listed above are acceptable.
2. Add the following line of Javascript in the first place where 
javascript is being used.
document.domain = "mydomainname.com";
After this, you're set to communicate between your two pages.
For more information, read about the document.domain property.
Code example - untested:
(Note that the document.domain property will return an error if you set 
it to something that isn't already part of your domain name.)
(http://www.mydomainname.com/index.html)
<html>
<script language="Javascript">
document.domain = "mydomainname.com";
var mywin = window.open("http://www2.mydomainname.com/index.html");
</script>
<body>
<form name="frm" id="frm" 
onsubmit="mywin.document.frm.txt.value=document.frm.txt.value;return 
false">
<input type="text" name="txt" value="">
<input type="submit">
</form>
</body>
</html>
(http://www2.mydomainname.com/index.html)
<html>
<script language="Javascript">
document.domain = "mydomainname.com";
</script>
<body>
<form name="frm" id="frm">
<input type="text" name="txt" value="">
</form>
</body>
</html>
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com