js跨域弹窗传值

本文介绍了如何解决在www.b.com网站的B.htm页面中嵌入www.a.com网站的A.htm页面,并从弹出的AA.htm页面跨域获取值的问题。通过创建一个不可见的iframe,其src指向B网站的BB.htm页面,该页面负责弹出AA.htm并传递值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     最近搞一个需求有关js跨域弹窗传值,问题是这样的:有两个网站分别为www.b.comwww.a.com 在B网站B.htm嵌入A网站的A.htm页面,在A.htm页面要从弹出的AA.htm页面获取一个值(产生跨域问题,AA.htm页面要弹出到B网站)。解决方法是创建一个不可见iframe,他的src为对方网站一个页面(BB.htm),在这个页面做弹出AA.htm页面),传值也依法泡制。代码如下(已下代码只作demo,神马弹出效果的无,代码写得也N丑^_^):

A网站A.htm页面内容

   1: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   2:  
   3: <html xmlns="http://www.w3.org/1999/xhtml">
   4: <head>
   5:     <title></title>
   6:     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
   1:  
   2:     <script type="text/javascript">
   3:         function Show() {
   4:             $("#ifShow").remove();
   5:             $("body").append("<iframe id='ifShow' src='http://www.b.com/BB.htm' style='display:none'></iframe>");
   6:         }
   7:         function SetValue(v) {
   8:             $("input[id='txtValue']").val(v);
   9:         }
  10:     
</script>
   7: </head>
   8: <body>
   9:     <input id="btnShow" type="button" value="弹窗" onclick="Show();" />
  10:     <input id="txtValue" type="text" disabled="disabled" />
  11: </body>
  12: </html>

A网站AA.htm页面内容

   1: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   2:  
   3: <html xmlns="http://www.w3.org/1999/xhtml">
   4: <head>
   5:     <title></title>
   6:     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
   1:  
   2:     <script type="text/javascript">
   3:         function SetValue() {
   4:             var v = $("input[id='txtValue']").val();
   5:             $("#ifSet").remove();
   6:             $("body").append("<iframe id='ifSet' src='http://www.b.com/BB.htm#" + v + "' style='display:none'></iframe>");            
   7:         }
   8:         $(document).ready(function () {
   9:             if (location.hash != "") {
  10:                 parent.parent.frames["Myiframe"].SetValue(location.hash.substr(1));
  11:             } 
  12:         });
  13:     
</script>
   7: </head>
   8: <body>
   9:     <input id="btnSetValue" type="button" value="确定" onclick="SetValue();return false;" />
  10:     <input id="txtValue" type="text" />
  11: </body>
  12: </html>

B网站B.htm页面内容

   1: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   2:  
   3: <html xmlns="http://www.w3.org/1999/xhtml">
   4: <head>
   5:     <title></title>
   6:     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
   7: </head>
   8: <body>
   9:     <iframe id="Myiframe" src="http://www.a.com/A.htm" height="50px" width="250px"></iframe>
  10: </body>
  11: </html>

B网站BB.htm页面内容(作为跨域的中间代理)

   1: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   2:  
   3: <html xmlns="http://www.w3.org/1999/xhtml">
   4: <head>
   5:     <title></title>
   6:     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
   1:  
   2:     <script type="text/javascript">
   3:         $(document).ready(function () {
   4:             if (location.hash != "") {
   5:                 parent.parent.$("#ifbSet").remove();
   6:                 parent.parent.$("body").append("<iframe id='ifbSet' src='http://www.a.com/AA.htm#" + location.hash.substr(1) + "' style='display:none'></iframe>");
   7:                 parent.parent.$("#ifbGet").remove();
   8:             } else {
   9:                 parent.parent.$("#ifbGet").remove();
  10:                 parent.parent.$("body").append("<iframe id='ifbGet' src='http://www.a.com/AA.htm' height='50px' width='250ox'></iframe>")
  11:             }
  12:         });
  13:     
</script>
   7: </head>
   8: <body>
   9:  
  10: </body>
  11: </html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值