使用iframe如何通过url传参把数据从一个页面传到另一个页面(contentWindow)

最近做的项目使用Iframe,使用起来感觉挺方便的。但是唯一感觉不开森的就是使用Iframe后,页面的地址栏的是不改变的哦,那么我们如何通过url将数据从一个页面传到另一个页面?

<div style="background-color: transparent;height: 580px;width: 1050px">
  <iframe name="studyFrame" allowtransparency="true" src="" width="100%" height="100%" 
  id="studyManageIframe" style="border: none;">
  </iframe>
</div>

记住我的iframe的id值哦——studyManageIframe
下面这段代码是获取iframe 中嵌套的页面的url参数的。

function GetIframeQueryString(name) {
   var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        /*var iframeSrc=window.parent.document.getElementById("studyManageIframe").contentWindow.location.href ;*/
   var r =window.parent.document.getElementById("studyManageIframe").contentWindow.location.search.substr(1).match(reg);
   if (r != null) {
      return decodeURI(r[2]);
        }
      return null;
    }

分解来说就是,

var iframeSrc=window.parent.document.getElementById("studyManageIframe").contentWindow.location.href ;`

可以把这个console一下,会发现这就是我们嵌套的在iframe中的那个页面的src哦,当然我们的参数也会一起出现的。
这里写图片描述
然后用上正则,哈哈,参数的值就得到了啊!
通过contentWindow 这里,返回的是iframe的window对象。

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值