Windowsを使用します.nameによるデータのドメイン間転送

9123 ワード

そのポイントは、
Stpe 1は、ブラウザがIframeに異域のページをロードし、このページはwindowに戻る.name="任意のデータ",このときiframeをとる.contentwindow.nameはアクセスを拒否します.
Step 2,Iframeに1つのドメイン内の任意のページをロードする,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; }