suppose I have a URL: https://whatever.com/#-Mquighthj23jjsn---12384959
How do I store -Mquighthj23jjsn
and 12384959
in two seperate variables
in javascript ???
CodePudding user response:
You can get the whole hash from the current url by calling window.location.hash
, so in your case you could get those variables in array like this:
const urlVariables = window.location.hash.substring(1).split('---');
So in result you'll have an with -Mquighthj23jjsn
and 12384959
.
CodePudding user response:
This is not a correct way to pass parameters in URL, because if we add or remove any parameter in the future, the code could break. As per standard URL parameter should be in Key Value Pair & separated with & characters. for example - https://abc.xyz/category?xyz&page?10.
Here is the code on how can you get parameter values by their key.
var url = new URL(window.location.href);
var c = url.searchParams.get("category");
var p = url.searchParams.get("page");
console.log(c);