On the Details page, use the URL object to get access to the url params, for example:

const url = new URL(window.location.href);
console.log(url.searchParams.get("id"));
Answer from Petr Broz on Stack Overflow
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ redirect-to-another-page-with-parameters-using-javascript
Redirect to another Page with Parameters using JavaScript | bobbyhadz
March 7, 2024 - Set the window.location.href property to redirect to the page with parameters. Here is the HTML for the example. ... Copied!<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2>bobbyhadz.com</h2> <button ...
๐ŸŒ
EncodedNA
encodedna.com โ€บ javascript โ€บ redirect-to-another-page-with-multiple-parameters-in-javascript.htm
How to Redirect to another Page with Multiple Parameters in JavaScript
May 20, 2025 - Now, to insert multiple parameters to the URL, youโ€™ll have to define parameters with values using the ? (Question mark) and & (ampersand) symbols before redirecting. For example, &ltscript> let redirectPage = () => { const url = "https://www yourwebsite com/content/ocncontent?userid=43&contentid=9"; window.location.href = url; } &lt/script> Similar example: How to Redirect Page after a small Delay using JavaScript?
Discussions

Redirect to url with hidden parameters - JavaScript - SitePoint Forums | Web Development & Design Community
I have had to jump off a validation project Im doing onto something else within the same project, but for some reason Im having a bit of trouble with something that seems so simple. I want to redirect the user to the next page if theyre log in details match, and take with it the ID of the user, ... More on sitepoint.com
๐ŸŒ sitepoint.com
0
November 3, 2016
javascript - How to 3xx redirect and pass a value from a table as a query parameter (in the URL)? - Stack Overflow
0 Go to specific dynamic url on click in table with specific cell data reference - Jquery ยท 0 how to pass an table row object directly to another html page as url argument? 1 How to pass and get Parameters between two Pages in Jquery Mobile? 0 Redirect to a page and pass the query parameters ยท 0 how to redirect a page in javascript ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Redirect to page and pass text and image parameter - HTML JS - Stack Overflow
I have a product page with 20 or so products on it. When you click on a product link I would like to pass 2 parameters to the page it redirects to, an image src and a text attribute and then display More on stackoverflow.com
๐ŸŒ stackoverflow.com
Get url params and redirect to url after
WARNING Because you are using URLs withing URLs, you might need to replace the special characters of the URL with encodeURIComponent(). More info on that here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent More on support.glitch.com
๐ŸŒ support.glitch.com
0
0
July 24, 2020
๐ŸŒ
Tutorial and Example
tutorialandexample.com โ€บ javascript-redirect-url-with-parameters
JavaScript redirect URL with parameters - TAE
March 19, 2022 - โ€œurlโ€ is any address to the website or the document we want to redirect to. ... Here, we are passing a URL to google search โ€œjavatpointโ€ and โ€œcppโ€, hence the URL takes two parameters, <!DOCTYPE > <html > <head> <title>Javascript redirect URL</title> </head> <body> <h3>After taking multiple parameters though hardcoding, we have the following information that you need:</h3><br /><br /> <input type="button" value="Search" onclick="Submit()" /> <script type="text/javascript"> function Submit(){ const url = "https://www.google.com/search?q=javatpoint&q=cpp"; window.location.href = url; }; </script> </body> </html>
๐ŸŒ
Talkerscode
talkerscode.com โ€บ howto โ€บ how-to-redirect-to-another-page-in-javascript-with-parameters.php
How To Redirect To Another Page In JavaScript With Parameters
Setting the href attribute of the window.location object is the most popular approach to redirect a URL in JavaScript. This simulates navigating to a new page in the conventional way. I don't believe any task could be easier than this: simply create a new URL. In jQuery, the replace() function of the window.location object can also be used to redirect to a web page. The current URL is removed from the history and replaced with a fresh URL using this method.
๐ŸŒ
Code Boxx
code-boxx.com โ€บ home โ€บ redirect with parameters in javascript (simple examples)
Redirect With Parameters In Javascript (Simple Examples)
July 3, 2023 - This tutorial will walk through examples of how to redirect with parameters in Javascript. Free example code download included.
๐ŸŒ
GitHub
gist.github.com โ€บ code-boxx โ€บ 98665062718708a91d2fece507b8666d
Javascript Redirect With Parameters ยท GitHub
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window.
๐ŸŒ
ScratchCode
scratchcode.io โ€บ home โ€บ redirect by javascript with example
Redirect By JavaScript With Example | Scratch Code
June 15, 2021 - In the above example, we are fetching the browser name using the navigator.appName and based on the browserโ€™s name we are redirecting to the appropriate page. Usually, we want to pass additional details with the URL which we have known as parameters or query strings.
Find elsewhere
๐ŸŒ
SitePoint
sitepoint.com โ€บ javascript
Redirect to url with hidden parameters - JavaScript - SitePoint Forums | Web Development & Design Community
November 3, 2016 - I have had to jump off a validation project Im doing onto something else within the same project, but for some reason Im having a bit of trouble with something that seems so simple. I want to redirect the user to the next page if theyre log in details match, and take with it the ID of the user, but hidden, but the redirect function isnt being called. if (data != 0){ var userID = data; $().redirect('index2.php', {'ID': userID}); }
Top answer
1 of 2
1

To pass both parameters, you may try this

jQuery(document).ready(function($){
    $('.product').click(function(event) {
        event.preventDefault();
        var name = $(this).data('title'), img = $(this).data('img')
        window.location = './lineup/index.html?title=' + name + '&img=' + img;
    });
});

To parse a value by key from url you can use this function (Source : MDN)

function loadPageVar (sVar) {
    return decodeURI(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURI(sVar).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
}

In your lineup/index.html put this code and the function given above

$(function(){
    $('#title-area').text(loadPageVar('title'));
    $('.product-img').text(loadPageVar('img')); // will set text

    // To set an image with the src
    $('.product-img').append($('<img/>', {
        'src':loadPageVar('img')
    }));
});
2 of 2
1

If you're looking for an alternative to URL query strings I'd look into window.sessionStorage object.

Store parameters like so:

$('.product').click(function(event) {
    event.preventDefault();
    window.sessionStorage.setItem('name', $(this).data('title'));
    window.sessionStorage.setItem('imgSrc', $(this).data('img'));
    window.location.reload(); //refreshes the page
});

Then to load the attributes, should they exist, add the following:

$(function(){
    if (window.sessionStorage.length){
        $('#title-area').text(window.sessionStorage.getItem('title'));

        $('.product-img').append($('<img/>', {
            'src':window.sessionStorage.getItem('imgSrc')
        }));
    }

    //include the click event listener for .product link here too
});
๐ŸŒ
ASPSnippets
aspsnippets.com โ€บ Articles โ€บ 2793 โ€บ Redirect-to-another-Page-with-multiple-Parameters-using-JavaScript
Redirect to another Page with multiple Parameters using JavaScript
May 3, 2019 - explained with an example, how to redirect to another Page with multiple Parameters using JavaScript. The multiple values to be passed to another Page will be added to the URL as QueryString parameters and then the Page will be redirected to another Page using window.location property in JavaScript.
๐ŸŒ
Glitch
support.glitch.com โ€บ coding help
Get url params and redirect to url after - Coding Help - Glitch Community Forum
July 24, 2020 - WARNING Because you are using URLs withing URLs, you might need to replace the special characters of the URL with encodeURIComponent(). More info on that here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
๐ŸŒ
Next.js
nextjs.org โ€บ docs โ€บ app โ€บ api-reference โ€บ functions โ€บ redirect
Functions: redirect | Next.js
1 month ago - By default, redirect will use push ... current URL in the browser history stack) everywhere else. You can override this behavior by specifying the type parameter....
Top answer
1 of 2
1

You can get the URL via JS very easily.

You have not provided the button click code in the question. So I am assuming there can be two ways to be redirected to another page;

  1. You can add a button and write a click function to redirect;

    <button onclick="redirect()"> Go To Page 2 </button>
    <script>
    function redirect()
    {
        var currentUrl = window.location.href;
        var parts = currentUrl.split("/");
        var param = parts[parts.length() -  1];
        window.location.hef = page2_url + "/" + param;
    }
    </script>
    
  2. You can also add an anchor tag for redirection;

    <a href="page2_url" id="redirectlink"> Button </a>
    <script>
      (function(){
          var currentUrl = window.location.href;
          var parts = currentUrl.split("/");
          var param = parts[parts.length() -  1];
    
          $("#redirectlink").attr("href", "page2_url" + "/" + param)
     })()
    </script>
    
2 of 2
0

You can use window.location.href to get the current URL of the page you're on. This will be stored as a string. You can then use .split('/') to turn your string into an array of parameters (where each index is split based on a /).

Eg:

"page.com/page1/food".split('/')

will yield:

const params = ["page.com", "page1", "food"]

To get the last element of this array you can use params.length-1 as the index, and then append this retrieved value onto the end of your redirected page. To redirect you set window.location.href equal to the new url.

See working example below:

$('#redirect').click(function() {
  const url = window.location.href; // 'https://stacksnippets.net/js'
  const params = url.split('/'); // ['https', '', 'stacksnippets.net', 'js']
  const parameter = params[params.length-1]; // 'js'
  
  const page2 = "https://www.example.com/" +parameter; // 'https://www.example.com/js'
  alert("Going to new page: " +page2);
  window.location.href = page2 // Go to page2 url
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="redirect">Click me</button>

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-redirect-to-another-webpage-using-javascript
How to Redirect to Another Webpage using JavaScript? | GeeksforGeeks
October 25, 2024 - T ... In this article, we will ... function used for performing the action is as follows. The setTimeout(callback, delay) function takes two parameters a callback ......