使用window.name 进行数据跨域传递
其中要点,
Stpe1,浏览器在Iframe中加载一个异域的页面,这个页面返回 <script>window.name=”任何数据”</script>,这时候,取 iframe.contentwindow.name是拒绝访问的。
Step2,在Iframe中加载一个本域中的任意页面,此时,iframe.contentwindow.name 就可以访问了,
DEMO: 下面是不修饰的代码,
1: <script type="text/javascript">
2:
3: function getContents(iframe) {
4: try {
5: // Make sure the iframe's window & document are loaded.
6: if (!iframe.contentWindow || !iframe.contentWindow.document) {
7: console.log("no contentwindow");
8: return null;
9: }
10:
11: console.info("window.name=" + iframe.contentWindow.name);
12: // Get the response from window.name
13: return iframe.contentWindow.name;
14: } catch (e) {
15: console.error(e);
16: return null;
17: }
18: }
19:
20:
21: //getContents(this);
22:
23:
24: function test() {
25: var doc = document;
26: var iframe = doc.createElement('iframe');
27: doc.body.appendChild(iframe);
28:
29: var form = doc.createElement('form');
30: doc.body.appendChild(form);
31:
32: var requestId = "__Go";
33: iframe.contentWindow.name = requestId;
34: form.target = requestId;
35: form.action = "http://castest.youxituan.com/cas/JsLogin";
36: form.method = "post";
37: var isFirst;
38: var time = 0;
39: iframe.onload = function () {
40: time++;
41: console.info("time=" + time);
42: if (time == 1) {
43: console.info("first" + iframe.src);
44: iframe.contentWindow.location = 'about:blank';
45:
46: //iframe.contentWindow.document.write(".....");
47: isFirst = false;
48: } if (time == 2) {
49: console.info("not first");
50: window.alert(iframe.contentWindow.name);
51:
52: //iframe.contentWindow.document.write('');
53: //iframe.contentWindow.close();
54: //document.body.removeChild(iframe);
55:
56: //iframe.src = '';
57: iframe = null;
58: }
59: console.log("onload");
60: getContents(iframe);
61:
62: };
63: iframe.onreadystatechange = function () {
64: console.log("onreadystatechange");
65: getContents(iframe);
66: };
67:
68: form.submit();
69: }
70:
71:
72: $(function () {
73: test();
74: });
75:
76:
77: </script>
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
最新文章
- web安全性测试用例
- winform label文本转换为图片 、Picturebox+label合并转换为图片
- 傻瓜式十分钟免费开启 HTTPS,是时候为你的站点加上小绿锁了
- hadoop2.0单机安装
- AWS AutoScaling
- SQLServer中临时表与表变量的区别分析
- 算法杂货铺——分类算法之朴素贝叶斯分类(Naive Bayesian classification)
- ios8中的UIScreen
- [Javascript] Ex: concatAll, map and filter
- CoFun 1612 单词分组(容斥)
- java——推断日期是否在今天之前
- python基础之 正则表达式,re模块
- Replace To Make Regular Bracket Sequence
- Vim常用的命令
- Java 枚举 的学习
- nodejs导出excel
- 大智慧F10离线资料压缩包带F10关键字过滤软件--更新于2014-01-06.rar 184MB
- C#数据库连接方法
- Spark向HDFS中存储数据
- Linux-数据库2