window.name cross-domain

In http://www.cnblogs.com/zhuzhenwei918/p/6759459.html this article, I mentioned several cross-domain way, here mainly on the use of cross-domain window.name.

   That agreement must be the same across domains, domain names, port numbers, we can get their content, to access it. 

 

  window.name this property is not a simple global properties --- just under one window, no matter how url changes, as long as the set up window.name, it has been the follow-up will not change, empathy, in an iframe, even if url window.name change, iframe is also a fixed value, take advantage of this, we can achieve a cross-domain.

  Here is localhost: 8088 / test2.html

Copy the code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>test2</title>
</head>
<body>
  <h2>test2页面</h2>
  <script>
    var person = {
      name: 'wayne zhu',
      age: 22,
      school: 'xjtu'
    }
    window.name = JSON.stringify(person)
  </script>
</body>
</html>

Copy the code

  That is, we want the data in it test2.html pass out, to localhost: 8081 / test1.html go. 

 

  Here is localhost: 8081 / test1.html

Copy the code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>test1</title>
</head>
<body>
  <h2>test1页面</h2>
  <iframe src="http://localhost:8088/test2.html" frameborder="1"></iframe>
  <script>
    var ifr = document.querySelector('iframe')
    ifr.style.display = 'none'
    var flag = 0;
    ifr.onload = function () {
        if (flag == 1) {
            console.log('跨域获取数据', ifr.contentWindow.name);
            ifr.contentWindow.close();
        } else if (flag == 0) {
            flag = 1;
            ifr.contentWindow.location = 'http://localhost:8081/proxy.html';
        }
    }
  </script>
</body>
</html>

Copy the code

       The intent here is clear, it is to use iframe to load test2.html over, because it is only in order to achieve cross-domain, so it will be hidden, but then have completed the most important step is to successfully set the iframe window.name , but now also can not get, because it is cross-domain, so we can put the src set to the current domain proxy.html.

  In addition, there is reason to set the flag, because every time you change location and they will do so again onload, so we hope that after acquiring the data, directly close (), so the use of this method.

 

This proxy.html reads as follows:

Press Ctrl + C to copy the code

 

Press Ctrl + C to copy the code

  Yes, nothing, of course, we can also replace src on test1.html, but such harm is obvious, is the need to test1.html data are loaded again, this is not what we want.

  

  

  OK! Thus, the successful completion of the cross-domain! 

  Code:  https://github.com/zzw918/cross-origin

 

  If we want to localhost: 8081 / test1.html have access to localhost: Data 8088 / test2.html is normal, because of cross-domain, it must be impossible to achieve the general idea can be like this.

Published an original article · won praise 0 · Views 4157

Guess you like

Origin blog.csdn.net/b_just/article/details/103597777