![](/img/trans.png)
[英]How do I add a stylesheet to an iFrame from the parent, using javascript?
[英]How do I capture a url parameter from parent page and send to an iframe using Javascript?
我在目標網頁上放置了一張iframed表格。 當用戶從會員鏈接訪問此登錄頁面時,會將參數放在URL(http:thelandingpage.com?c3 = 1)中。 當他們提交iframe表單時,我需要將該變量(c3 = 1)發送到下一頁。 我該如何實現?
這是我正在探索的選項:
從父頁面網址中提取參數
注意:此代碼被放置在iframe中
(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)
<strong>If variable displays, that's a success!</strong>
<br>
c3: <span id="c3"></span><br>
當然,它不能正常工作。 它在iFrame的“ src”中顯示第一個參數,而不是父頁面的第一個參數。
在獲取“ c3 = 1”參數之前,我無法繼續進行操作。但是,我的下一步是將變量保留在隱藏字段或其他內容中,以便將其作為查詢字符串發送到下一頁。
請幫忙!
解決方案1:將參數直接傳遞給iframe
元素。
家長代號:
window.onload=function(){
iframe=document.createElement("iframe");
iframe.src="iframe.php?"+window.location.href.split"?")[1];
document.body.appendChild(iframe);
}
解決方案2:在父查詢中查找url
查詢:
iframe:
window.postMessage("url?","parentsurl");
window.addEventListener("message",function(e){
if(e.data.url){
Parentsurl=e.data.url;
}
});
上級:
window.addEventListener("message", function(e) {
if(e.data="url?"){
window.postMessage({url:window.location.href});
}
});
請參閱: 從iframe訪問父URL
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.