其中要点,

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; }

使用window.name 进行数据跨域传递的更多相关文章

  1. HTML5 window/iframe跨域传递消息 API

    原文地址:HTML5′s window.postMessage API 在线示例:Using HTML5's window.postMessage(请打开控制台看日志) 原文日期: 2010年09月0 ...

  2. javascript跨域传递消息 / 服务器实时推送总结

    参考文档,下面有转载[非常好的两篇文章]: http://www.cnblogs.com/loveis715/p/4592246.html [跨源的各种方法总结] http://kb.cnblogs. ...

  3. window.name实现的跨域数据传输 JavaScript跨域总结与解决办法

    原文地址:  http://www.cnblogs.com/rainman/archive/2011/02/20/1959325.html#m4 什么是跨域 1.document.domain+ifr ...

  4. node下的跨域传递cookie

    研究背景: 最近有一位朋友找工作,需要面试,涉及到面试就涉及面试题,于是我想起来鄙人之前面试被问到的一个跨域传递cookie的问题.搜索了相关资料,但自己不敲一下肯定是不足以让人信服的. 我用node ...

  5. window.name实现的跨域数据传输

    这篇文章是对 JavaScript跨域总结与解决办法 的补充. 有三个页面: a.com/app.html:应用页面. a.com/proxy.html:代理文件,一般是一个没有任何内容的html文件 ...

  6. javascript跨域通信(二):window.name实现的跨域数据传输

    首先了解一下window.name这个东西是什么. name 在浏览器环境中是一个全局/window对象的属性,当在 frame 中加载新页面时,name 的属性值依旧保持不变 并且name 属性仅对 ...

  7. ajax请求json数据跨域问题(转)

    一.后台代理技术 由服务器端向跨域下的网站发出请求,再将请求结果返回给前端,成功避免同源策略的限制. 具体操作如下: 1.在localhost:81/a.html中,向同源下的某个代理程序发出请求 $ ...

  8. jsonp实现数据跨域请求

    1.我们知道,哪怕跨域js文件中的代码(当然指符合web脚本安全策略的),web页面也是可以无条件执行的. 远程服务器remoteserver.com根目录下有个remote.js文件代码如下: al ...

  9. 关于前端调用后端php数据跨域的问题

    https://blog.csdn.net/qq_21386275/article/details/87269979 js前端 <!DOCTYPE html><html>< ...

随机推荐

  1. errant-transactions

    https://www.percona.com/blog/2015/12/02/gtid-failover-with-mysqlslavetrx-fix-errant-transactions/ 使用 ...

  2. 201671010140. 2016-2017-2 《Java程序设计》java学习第二周

                                 学习第二周(Java基本程序设计结构)      这一周,着重学习了Java的简单程序设计实现及运行,通过自己操作,发现Java的程序语法大面 ...

  3. Flask 上下文(Context)原理解析

    :first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; m ...

  4. Android 建立AIDL的步骤

    建立AIDL服务要比建立普通的服务复杂一些,具体步骤如下: (1)在Eclipse Android工程的Java包目录中建立一个扩展名为aidl的文件.该文件的语法类似于Java代码,但会稍有不同.详 ...

  5. Python_14-绘图

    v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...

  6. PHPMailer fe v4.11 For Thinkphp 3.2

    PHPMailer fe v4.11 For Thinkphp 3.2,你值得拥有! 今晚用TP3.2开发一个东西的时候需要邮件发送功能,理所当然的想到了PHPMailer.于是有了此文!------ ...

  7. 4.std::string中库函数的使用。

    为了美观,我们把输入和输出设计成如下: #include <iostream> #include <string> int main() { std::string name; ...

  8. Oracle数据库之单表查询

    接着上一篇的分享,今天主要给大家分享的是关于数据中的单表查询,单表查询很基础,也很重要,但是任何一个初学者必须要掌握的姿势,单表查询就是对单个表进行操作,查询我们想要的数据.单表查询里面的内容也是比较 ...

  9. java IO 对象流 反序列化和序列化

    例: 重点:需要序列化的对象必须实现Serializable接口 //需要序列化的对象 public class User implements Serializable { private Stri ...

  10. jQuery bind() live()

    <script type="text/javascript"> $(document).ready(function () { /*$('.clickme').live ...