跨域 iframe 实例 (Cross-Domain Javascript execution library)

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> 
 

Exec file:

 

<html>
<body>
<script type="text/javascript" src="crossdomain_server.js"></script>
<script type="text/javascript">
crossdomain.server().init();
</script>
</body>
</html> 
 

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> 
 

 

Download : CrossDomain

 

来源: http://www.emposha.com/javascript/cross-domain-javascript-execution-library.html

 

 

 

 

 

 

你可能感兴趣的:(JavaScript)