How can I get the params from the URL path as we do in Django?
For example if the url is domain.dom/userid,
then we need userid as a param in js and redirect to domain.dom.
Is this possible in javascript?
3 Answers
Try this
let url = new URL("https://domain.dom/freddy_user"); // new Url(location.href)
const userid = url.pathname.slice(1);
url.pathname="index.html";
url.searchParams.set("userid",userid)
console.log(url); // location.replace(url)
6 Comments
Nishant Singhal
on which page should i write this code so that object not found error is not shown
mplungjan
You put this in the head of the page that receives the userid- you uncomment my comments and remove the test strings
Nishant Singhal
but it doesn't recieve the request with the error object not found
mplungjan
I have no idea what you mean
mplungjan
I have no idea how your server works. You were the one giving very little info on how you pass the userid. I just came up with something to not confuse userid=userid - it seems you need to play with htaccess instead of JavaScript
|
If you need the current URL and params, this could help you
I'am considering that the userID param is the first param like:
url.com/123
where 123 is the userID
//Get current base URL
let base_url = window.location.origin;
//Get userID or first param
//Change number 3 if your param is in another position
let userId = window.location.href.split("/")[3];
alert(userId);
//Redirect to base URL
window.location.href = base_url;
location.replace("someurl")domain.dom/useridI want it to go todomain.dom/index.htmlwith the parameter userid