[英]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.