2014-05-08 92 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的解决方案也能很好地工作。 –

相关问题