跨域 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" οnclick="sendText()" />
</div>
</body>
</html> 
 

 

Download : CrossDomain

 

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

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值