美文网首页
跨域-Hash

跨域-Hash

作者: duJing | 来源:发表于2017-01-04 14:57 被阅读57次
问题描述:
使用location.hash + iframe实现跨越
现在本地页面a.html要访问异域b.html的数据
怎么办?
环境配置:
a.html 和 c.html我们放在php的wamp中运行
他们的地址为:http://localhost/a.html和http://localhost/c.html
b.html 的代码,我放在lamport.me/b.html中

原理:
1.a.html中有一个隐藏的frame,该frame指向异域lamport.me的b.html,且传递hash值给b.html
2.b.html获取hash值,生成data值,然后动态创建iframe,该iframe将data值传给与a.html同域的c.html
3.因为c.html与a.html同域,可以传值。
如果该文件不能访问,你可以在自己的wamp中配置一个虚拟主机进行访问
虚拟主机的配置地址:
http://blog.csdn.net/super_yang_android/article/details/53991982
本地http://localhost/a.html
<!doctype html>
<html>
<head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
    <title>localhost:a.html</title>

    <style type="text/css">

    </style>
</head>

<body>
<script type="text/javascript">
    alert('我是a页面');
    function sendRequest(){
        //动态创建个iframe
        var ifr = document.createElement('iframe');
        ifr.style.display = 'none';
        //跨域发送请求给b.html,参数是#sayHello
        ifr.src = 'http://lamport.me/b.html#sayHello';
        document.body.appendChild(ifr);
    }
    function checkHash(){
        var data = location.hash?location.hash.substring(1):'';
        if(data){
            //这里处理返回值
            alert(data);
            location.hash = '';
        }
    }
    setInterval(checkHash,1000);
    window.onload = sendRequest;
</script>
</body>
</html>
异域http://www.lamport.me/b.html
<!doctype html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<title>另某个域的:b.html</title>

<style type="text/css">

</style>
</head>

<body>
<script type="text/javascript">
function checkHash(){
    var data = '';
    //模拟一个简单的参数处理操作
    switch(location.hash){
        case '#sayHello':
              data = 'HelloWorld';
              break;
        case '#sayHi':
              data = 'HiWorld';
              break;
        default : break;
    }
    data && callBack('#'+data);
}
function callBack(hash){
   var proxy = document.createElement('iframe');
   proxy.style.display = 'none';
   proxy.src = 'http://localhost/c.html'+hash;
   document.body.appendChild(proxy);
}
window.onload = checkHash;
</script>
</body>
</html>
本地http://localhost/c.html
<!doctype html>
<html>
<head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
    <title>localhost:c.html</title>
</head>

<body>
<script type="text/javascript">
    //因为c.html和a.html属于同一个域,所以可以通过改变其location.hash的值,可以通过parent.parent获取a.html的window对象
    parent.parent.location.hash = self.location.hash.substring(1);
</script>
</body>
</html>

相关文章

  • 前端常见跨域解决方案

    一、通过Jsonp跨域二、document.domain+iframe 跨域三、location.hash+ifr...

  • 跨域-Hash

  • 跨域通信

    跨域通信常用的几种方式 1、JSONP 2、WebSocket 3、CORS 4、Hash 5、postMessa...

  • iFrame跨域的方式

    4种通过iframe跨域与其他页面通信的方式 不同域下的iframe不能进行操作。 1、location.hash...

  • 深入跨域问题(3) - 利用 JSONP 解决跨域

    深入跨域问题(1) - 初识 CORS 跨域资源共享;深入跨域问题(2) - 利用 CORS 解决跨域深入跨域问题...

  • 关于设置env等环境变量的思考

    1、如何处理跨域后台处理跨域前端处理跨域浏览器处理跨域 前端本地处理跨域:代理线上跨域的处理方式:Nginx反向代...

  • Web前后端跨域问题处理

    跨域问题有前台跨域(iframe间)和后台跨域。 前台跨域的解决方案可以采用跨域文档通讯(Cross domain...

  • 跨域

    跨域 什么是跨域: 解决跨域 通过jsonp原理:在页面引入跨域js和css时,没有存在跨域问题.因此可以动态创建...

  • 跨域问题详解分析

    参考文档 CORS详解 跨域资源共享 CORS 详解 js中几种实用的跨域方法原理详解 跨域的那些事儿 跨域与跨域...

  • 跨域问题:好几种解决方案

    跨域分为广义跨域和狭义跨域 广义跨域:一个域下的文档或脚本试图去请求另一个域下的资源; 广义跨域可以分为以下几种:...

网友评论

      本文标题:跨域-Hash

      本文链接:https://www.haomeiwen.com/subject/lrwovttx.html