« Orinoco Silver+Gold and Cabletron Enterasys Roamabout cards support WPA after all | Broken packed javascript libraries » |
Most browsers adhere to the “same-origin” policy, which prevents AJAX calls between different servers identified by their host name as a security measure.
There have been a number of approaches used to solve this problem and here is a brief list of them.
From the client side, depending on the browser, you can lower browser security settings to allow for cross site / cross domain ajax.
If the two sites are sub domains on the same domain, use document.domain to make them use the same super domain.
All the above solutions have different requirements that bring about various other problems. Hence I had to come up with another approach for a project based on ideas from a discussion with a couple of colleagues.
This approach would ideally:
The following diagram illustrates how this approach works with iframes to manage call backs to an iframe.
From the diagram above, you can see the setup comprises of the following,
So here’s how it all ties together, allowing webpage A to access webpage B’s dom objects and JavaScript functions and variables.
Webpage A loads up with 2 iframes A and C which contain Webpage B and the Console frame respectively. One important point to note is that the Console frame is a page on the same server as Webpage B while the Control frame is a page on Webpage A.
This is further illustrated with the following code extracts.
<iframe name="iframeA" src="http://webserverB/Webpage_B.html">
<iframe name="iframeC" src="http://webserverB/Console_Frame.html">
<script type="text/javascript">
var variable1="Tadarrr, it works!";
</script>
<script type="text/javascript">
var variable1="Tadarrr, it works!";
</script>
<script type="text/javascript">
</script>
Here’s a live example to top it all off.
To communicate back from webserverB to webserverA, it can be summarised as follows.