日期:2014-05-16  浏览次数:20492 次

iframe 跨域子页面操作父页面的问题,真心求教
现在要完成一个上传视频的功能,服务器为两台,a 为网站服务器,b为视频服务器,在b中有个上传插件,我于是在a中建立了一个页面a.html(实际上这个页面是CMS动态生成的),在a.html中嵌入了iframe,src就是b中的插件页面b.php,这样来完成上传功能,但是现在有个需求需要b.php返回一个上传路径给a.html,由于跨域,是无法直接通过parent直接将 数据 传递给a.html,于是又在a服务器上建立了一个页面c.html,通过b.php中嵌入了一个iframe,src指向c.html,这样上传路径通过location.hash传递给了c.html,c.html通过定时器检测hash变化,得到了从b.php传递过来的数据,但是现在问题出现了,c.html如何将这个数据传递给a.html呢,通过top,通过parent.parent都不能将数据传递过去,在chrome下控制台发现parent.parent能够得到a.html的路径,但是报错
Unsafe JavaScript attempt to access frame with URL http://localhost/lszfw/e/admin/AddNews.php?enews=AddNews&classid=62 from frame with URL http://localhost/lszfw/e/admin/ShowClassNav.php?ecms=1. The frame being accessed set 'document.domain' to 'localhost', but the frame requesting access did not. Both must set 'document.domain' to the same value to allow access.


不知道c.html如何将得到的数据传递给a.html呢?
------解决方案--------------------
举个例子:我这个应用是在A域名下的一个页面a.html中有个iframe,src为B域名下的b.html,因为b.html的内容高度会比a.html中初始设置的iframe高度要高一些,所以希望在b.html加载完毕后能自动调节iframe的高度。

实现方法:http://www.b.com/B.html 加载完毕后,计算高度同时通过hash传给http://www.a.com/setHeight.html
,完后在http://www.a.com/setHeight.html中通过parent.parent.document.getElementById去控制http://www.a.com/a.html里的iframe的高度


我是这么做的:
A.html   所在域名  www.a.com
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
    <meta content="text/html; charset=gb2312" http-equiv="Content-Type">  
</head>  
    <body>  
    <iframe height='200px' id='iframe1' src="http://www.b.com/B.html"></iframe>  
</body>  
</html> 



B.html     所在域名  www.b.com

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    <html xmlns="http://www.w3.org/1999/xhtml">  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
    </head>  
    <body style="background-color:#ccc">  
        <div style="height:600px;">sdfsfsssfsfs</div>  
        <script>  
            window.onload = function()
{
                        //先判断iframe是否存在,存在的话,删除掉先
if(document.getElementById("sheight"))
{
document.body.removeChild(document.getElementById("sheight"));
}
var iframe = document.createElement("iframe");
iframe.width='0px';
iframe.height='0px';
iframe.id='sheight';
var currentheight = document.body.scroll