简体   繁体   English

如何从父页面捕获url参数并使用Javascript发送到iframe?

[英]How do I capture a url parameter from parent page and send to an iframe using Javascript?

I have an iframed form placed on a landing page. 我在目标网页上放置了一张iframed表格。 When users come to this landing page from an affiliate link, a parameter is placed in the url (http:thelandingpage.com?c3=1). 当用户从会员链接访问此登录页面时,会将参数放在URL(http:thelandingpage.com?c3 = 1)中。 When they submit the iframe form, I need to send that variable (c3=1) to the next page. 当他们提交iframe表单时,我需要将该变量(c3 = 1)发送到下一页。 How could I achieve that? 我该如何实现?

Here is the option I'm exploring: 这是我正在探索的选项:
Pulling the parameter from the parent page url 从父页面网址中提取参数
Note: this code is being placed inside the iframe 注意:此代码被放置在iframe中
(Javascript) (JavaScript)

<script language="JavaScript">
  function processUser()
  {
    var parameters = window.parent.location.search.substring(1).split("&");

    var temp = parameters[0].split("=");
    c3 = unescape(temp[1]);

    //Assign "c3=" to a var
    var c3variable = c3;

    //Append c3 Variable
    document.getElementById("c3").innerHTML = c3variable; 

  }
processUser();


</script> 

(HTML) (HTML)

<strong>If variable displays, that's a success!</strong>
<br>
c3: <span id="c3"></span><br>

Of course it's not working properly though. 当然,它不能正常工作。 It displays the first parameter in the iFrame's "src", not the first parameter of the parent page. 它在iFrame的“ src”中显示第一个参数,而不是父页面的第一个参数。

I can't move on until I grab the "c3=1" parameter.. But my next step would be to stick that variable in a hidden field or something in order to send it to the next page as a query string. 在获取“ c3 = 1”参数之前,我无法继续进行操作。但是,我的下一步是将变量保留在隐藏字段或其他内容中,以便将其作为查询字符串发送到下一页。

Please help! 请帮忙!

Solution 1: pass parameter directly to the iframe element. 解决方案1:将参数直接传递给iframe元素。

Parents code: 家长代号:

window.onload=function(){
    iframe=document.createElement("iframe");
    iframe.src="iframe.php?"+window.location.href.split"?")[1];
    document.body.appendChild(iframe);
}

Solution 2: lookup the parent for url query: 解决方案2:在父查询中查找url查询:

Iframe: iframe:

window.postMessage("url?","parentsurl");
window.addEventListener("message",function(e){
    if(e.data.url){
        Parentsurl=e.data.url;
    }
});

Parent: 上级:

window.addEventListener("message", function(e) {
    if(e.data="url?"){
        window.postMessage({url:window.location.href});
    }
});

See: Access parent URL from iframe 请参阅: 从iframe访问父URL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 javascript 从父级向 iFrame 添加样式表? - How do I add a stylesheet to an iFrame from the parent, using javascript? 使用ajaxSend捕获iframe中父页面的请求 - Using ajaxSend to capture requests from parent page within an iframe 我如何引用的文档对象 <iframe> 从父页面,使用jQuery? - How do I refer to the document object of an <iframe> from the parent page, using jQuery? 如何根据iframe的网址是否更改来更改父页面的网址? - How do I change parent page url based on if url of iframe is changed? 如何使用Javascript从iframe实施跨域URL访问? - How do I implement Cross Domain URL Access from an Iframe using Javascript? 如何在加载iframe位置网址之前捕获它? - How do I capture an iframe location url before it is loaded? 如何使用 javascript 获取 URL 参数并将其添加到同一页面上的链接? - How do I get a URL parameter and add it to a link on the same page using javascript? 在父级中单击某些内容时,如何将URL地址从父级传递到iframe? - how do i pass a URL address from a parent to iframe when clicking on something in the parent? 如何使用Javascript从iframe编辑索引页的innerHTML? - How do I edit the innerHTML of an index page from an iframe using Javascript? 如何将div宽度从iframe发送到父页面 - How to send the div width from an iframe to the parent page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM