document.domain 跨域实现

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset='utf-8'/>
 5     <title>document.domains</title>
 6 </head>
 7 <body>
 8 <p>我是www页面</p>
 9 <iframe src="http://test.mzk.com/domains.html" id='ifm'></iframe>
10 </body>
11     <script type="text/javascript">
12     document.domain='mzk.com';
13         function alt(){
14             
15         }
16     var ifm=document.getElementById('ifm');
17     ifm.onload = function(){
18     var doc = ifm.contentDocument || ifm.contentWindow.document;
19     // 在这里操纵b.html
20     doc.getElementById('btn').onclick=function(){
21         alert('我是www页面');
22     }
23 };
24     </script>
25 </html>

上面是http://www.mzk.com/damains.html;

 http://test.mzk.com/domains.html页面是:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset='utf-8'/>
 5     <title>document.domains</title>
 6 </head>
 7 <body>
 8 <input type="button" class="btn" id="btn" value="按钮" >
 9 </body>
10     <script type="text/javascript">
11     document.domain='mzk.com';
12     </script>
13 </html>

 

posted @ 2016-05-26 14:03  honely1314  阅读(150)  评论(0编辑  收藏  举报