In one of the projects I am faced with problem of the java scripts execution on different domains.
After advice from one of team leaders, the solution was found and it was a quite simple.
For example, we have A domain with frame that point to domain B which is to call javascript function in domain A.
If you simply call a window.parent.funcame you will get exception. So what can we do ?
It is very simple – in domain B we create frame to domain A and then we try to execute a function on domain A as window.parent.parent.fucname and how we can see function is executed succesufully.
So this script does this operation automatically without requiring knowledge of java script.
Credits: Nir Levy
Usage:
Domain A:
Main file:
<html>
<head><title>domain_a</title></head>
<body>
<script>
function showmelove(text)
{
alert(text);
}
</script>
... some content here ...
<iframe id="hpel" height="100" frameborder="0" width="100%" scrolling="0" title="" src="http://domain_b/iframe.html" style="height: 194px;"></iframe>
... some content here ...
</body>
</html>
* This source code was highlighted with Source Code Highlighter.
Exec file:
<html>
<body>
<script type="text/javascript" src="crossdomain_server.js"></script>
<script type="text/javascript">
crossdomain.server().init();
</script>
</body>
</html>
* This source code was highlighted with Source Code Highlighter.
Domain B:
<html>
<head>
<script src="crossdomain_client.js" type="text/javascript" ></script>
<script>
crossdomain.client("frame").init("http://www.domain_a/iframe.html");
function sendText() {
crossdomain.client("frame").callfunc("showmelove",{mesasge: "'hello world'"});
}
</script>
</head>
<body>
<div id="allBodyDiv">
some content goes here...
<input type="button" value="send" onclick="sendText()" />
</div>
</body>
</html>
* This source code was highlighted with Source Code Highlighter.
Download: CrossDomain
How to use crossdomain.client(“frame”).callfunc(“showmelove” ) to pass an existing js variable/object/array from Domain B: to Domain A?
Yes, it will work. But i want to do the same for ning, facebook or some third party. But, they will not accept to copy the frame.html in domain A. Then what’s the solution? Is it possible just copy an iframe Code in Domain A and reference to a single javascript file?