Window.name实现跨域
2016-04-14 23:08
387 查看
前面讲过用domain+frame实现跨域,下面讲另一种跨域的方法。
之所以可以用window.name实现跨域,最重要的一点是在一个frame中重新载入页面时window.name属性值在前后两个页面都是共享的,因此我们可以载入一个新的页面,然后设置这个name,之后在载出,那么我们就可以获取到跨域页面传给name的值啦。当然用完了iframe就把它删啦,因为name属性是不安全的,其他的页面也会修改name属性,反正也没用啦。
可参照这篇文章
之所以可以用window.name实现跨域,最重要的一点是在一个frame中重新载入页面时window.name属性值在前后两个页面都是共享的,因此我们可以载入一个新的页面,然后设置这个name,之后在载出,那么我们就可以获取到跨域页面传给name的值啦。当然用完了iframe就把它删啦,因为name属性是不安全的,其他的页面也会修改name属性,反正也没用啦。
可参照这篇文章
//www.newDomain.com/newDomain1/test.html <!doctype html> <html> <head> <title>newDomain1</title> <style type="text/css"> </style> </head> <body> <div id="mzz"> <h1>this newDomain1</h1> </div> <script> var ifr=document.createElement('iframe'); //设置跨域的url ifr.src="http://www.script.newDomain.com/newDomain2/test.html"; ifr.style.display="none"; document.body.appendChild(ifr); var doamin2cookie; var isFirst=true; var xx; ifr.onload=function(){ if(isFirst){ //重新加载url,从跨域的状况回到非跨域的情况 ifr.contentWindow.location = 'http://www.newDomain.com/newDomain1/test.html'; isFirst = false; }else{ console.log(ifr.contentWindow.name); //setCookie("name",ifr.contentWindow.name); ifr.contentWindow.document.write(''); ifr.contentWindow.close(); document.body.removeChild(ifr); ifr.src = ''; ifr.onload=null; } } </script> </body> </html
<!doctype html> <html> <head> <title>newDomain1</title> <style type="text/css"> </style> </head> <body> <div id="zzq"> <h1>this newDomain2</h1> </div> <script> window.name="我是newDomain2"; </script> </body> </html>
相关文章推荐
- 探秘ajax跨域请求
- 基于iframe实现ajax跨域请求 获取网页中ajax数据
- ie7下利用ajax跨域盗取cookie的解决办法
- javascript跨域的方法汇总
- 在Ajax中使用Flash实现跨域数据读取的实现方法
- AJAX实现跨域的三种方法(代理,JSONP,XHR2)
- js实现跨域访问的三种方法
- 如何设置iframe高度自适应在跨域情况下的可用方法
- 使用script的src实现跨域和类似ajax效果
- js跨域问题之跨域iframe自适应大小实现代码
- JavaScript两种跨域技术全面介绍
- ajax跨域(基础域名相同)表单提交的方法
- JS不能跨域借助jquery获取IP地址的方法
- jQuery跨域问题解决方案
- js中跨域方法原理详解
- 使用HTML5中postMessage知识点解决Ajax中POST跨域问题
- Ajax异步(请求)提交类 支持跨域
- jQuery使用ajax跨域请求获取数据
- 使用$.getJSON实现跨域ajax请求示例代码
- 有关Ajax跨域问题的两种解决方法