简体   繁体   中英

Pass URL parameters through to next page with Javascript (or not)

I think this is simple but can't seem to find the answer.

In our autoresponder, when a contact clicks a link to visit a page, the contact id and email are passed through to the next page.

So by clicking an email, the contact lands on page.com/1/?id=123&email=name@gmail.com

On that page, and there is a button to click to go to the next page...

what do I need to do so the parameters pass to the next page and the contact lands on page.com/2/?id=123&email=name@gmail.com?

Thanks for the help!

If the page number is all that changes:

button.addEventListener('click', () => {
  const currentPage = Number(window.location.href.match(/\d+/));
  window.location.href = window.location.href.replace(/\d+/, currentPage + 1);
});

Edit: here is a function to get the querystring parameters from This link

    function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));

}

If you already know the base url, and have the parameters just combine the two and redirect.

document.getElementById("myButton").onclick = function () {
     var url = "page.com/2"; //get your base url how ever you're doing it.
     var queryString = "?id=123&email=name@gmail.com";
     var fullUrl = url + queryString;
     window.location.href = fullUrl ;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM