admin管理员组

文章数量:1324844

Trying to do the following:

Store params from url, i.g. mydomain/page.html?cid=123456

If a user clicks on a button (they have a class of .btn-cta) it will take the params ?cid=123456 and add them to the new page those buttons link to /tour.html

I'm currently doing 1/2 of that with passing the params to an iframe on the page, now I need to get the above part working:

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

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

Trying to do the following:

Store params from url, i.g. mydomain./page.html?cid=123456

If a user clicks on a button (they have a class of .btn-cta) it will take the params ?cid=123456 and add them to the new page those buttons link to /tour.html

I'm currently doing 1/2 of that with passing the params to an iframe on the page, now I need to get the above part working:

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

iframe.src = iframe.src + '?' + params;
Share Improve this question asked May 8, 2014 at 17:47 Sal BSal B 5401 gold badge5 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Here's how I'd do it using jquery:

     $('.btn-cta').each(function(i, el){
        let $this = $(this); // only need to create the object once
        $this.attr({
            href: $this.attr("href") + window.location.search
        });
    });

And in Vanilla ES2015

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

This takes all the elements that have class .btn-cta and appends the page query string to each of their href attributes.

So if the page url is `http://domain/page.html?cid=1234

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

bees

<a href="/tour.html?cid=1234" class="btn-cta">Tour</a>
<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>

本文标签: javascriptPass URL Parameters from one page to anotherStack Overflow