2014-05-08 91 views
1

試圖執行以下操作:將URL參數從一個頁面傳遞到另一個頁面

從url中存儲參數,例如, mydomain.com/page.html?cid=123456

如果用戶點擊一個按鈕(它們有一個.btn-cta類),它將採用params?cid = 123456並將它們添加到新頁面按鈕鏈接到/tour.html

目前我做的1/2與傳遞PARAMS的iframe頁面上,現在我需要得到上面的部分工作:

var loc = window.location.toString(), 
    params = loc.split('?')[1], 
    iframe = document.getElementById("signupIndex"), 
    btn = $('.btn-cta'); 

iframe.src = iframe.src + '?' + params; 

回答

2

這裏的我怎麼會用jQuery來做:

$('.btn-cta').each(function(i, el){ 
    $(this).attr({ 
     href: $(this).attr("href") + window.location.search 
    }); 
}); 

而在香草ES2015

document.querySelectorAll('.btn-cta') 
    .forEach((el) => el.attributes.href.value += window.location.search); 

這需要所有具有.btn-cta類和追加頁面查詢字符串到他們的每一個href屬性的元素。

因此,如果網頁的網址爲`http://domain/page.html?cid=1234

<a href="/tour.html" class="btn-cta">Tour</a> 

成爲

<a href="/tour.html?cid=1234" class="btn-cta">Tour</a> 
+0

起初並不工作,但我想通了,我需要添加'周圍的href –

+1

沒錯,編輯爲添加'「」' –

1
<html> 
<head> 
<script src="js/jquery.js"></script> 
<script> 
$(document).ready(function() { 
var loc = window.location.href; 
var params = loc.split('?')[1]; 
$(".btn-cta").click(function(){ 
window.open("tour.html?"+params,'_self',false); 
}); 

}); 
</script> 
</head> 
<body> 

<button type="submit" class="btn-cta">Click Me</button> 


</body> 
</html> 
+0

我正準備用.click()函數向這個方向前進,但@patrick的解決方案也能很好地工作。 –

相關問題