jquery - Redirect to another page and pass parameter via JavaScript - Stack Overflow
javascript - How to redirect to an html page with parameters? - Stack Overflow
javascript - How do I redirect to another webpage? - Stack Overflow
How do I redirect with JavaScript? - Stack Overflow
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"));
You can get the parameters from the url in your details page with the following code:
var url = new URL(window.location);
var param = url.searchParams.get("parameterName");
As @Michael Hurley points out, url is not available in some browsers (namely IE), so if you need to support them you'll have to do some additional work:
exp = /(?:parameterName=)(\w)/
match = str.match(exp)
//match[1] would hold the value of 'parameterName'
You need to remove the slash between the page name and the question mark, otherwise the web server will think you are requesting a folder named /page2.html/, hence you are getting 404 - not found
localhost/page2.html?numberID=5
var number = 5;
window.location.href = "page2.html?number="+number;
One does not simply redirect using jQuery
jQuery is not necessary, and window.location.replace(...) will best simulate an HTTP redirect.
window.location.replace(...) is better than using window.location.href, because replace() does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.
If you want to simulate someone clicking on a link, use location.href
If you want to simulate an HTTP redirect, use location.replace
For example:
// similar behavior as an HTTP redirect
window.location.replace("https://stackoverflow.com");
// similar behavior as clicking on a link
window.location.href = "https://stackoverflow.com";
WARNING: This answer has merely been provided as a possible solution; it is obviously not the best solution, as it requires jQuery. Instead, prefer the pure JavaScript solution.
$(location).prop('href', 'http://stackoverflow.com')
To redirect to another page, you can use:
window.location = "http://www.yoururl.com";
window.location.replace('http://sidanmor.com');
replace() does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco.
If you want to simulate someone clicking on a link, use
window.location.hrefIf you want to simulate an HTTP redirect, use
window.location.replace
For example:
// similar behavior as an HTTP redirect
window.location.replace("http://sidanmor.com");
// similar behavior as clicking on a link
window.location.href = "http://sidanmor.com";
Taken from here: How to redirect to another page in jQuery?
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;
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>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>
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>