I am tracking URLs with "posName" parameters like:
example.com?posName=Content+&+Community+Manager+(H/F)
But my code returns "Content" only:
function () {
var uri = document.location.href;
var uri_dec = decodeURIComponent(uri);
var url = new URL(uri_dec);
var posName= url.searchParams.get("posName");
return posName;
}
How can I get the full parameter?
EDIT: I have another URL like:
exemple.com?posName=Club+&+Animator+(H/F)
And the code returns the full parameter... So is it the length of the parameter which causes this issue?
For getting the URL parameters, there are 2 ways: By using the URLSearchParams Object. By using Separating and accessing each parameter pair.
URL parameters are commonly used to sort content on a page, making it easier for users to navigate products in an online store. These query strings allow users to order a page according to specific filters and to view only a set amount of items per page. Query strings of tracking parameters are equally common.
Using &url='+encodeURIComponent(url); to pass a URL from browser to server will encode the url but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.
Even if you use the post method instead of the get method to remove parameters from the url. You can still see the passed parameters in the request message. The way to safely hide parameters is to encrypt them. You can refer to this link for an example of how to encrypt the parameters in the URL.
I've modified your function to accept the URI as an argument. You need to encode the parameter before appending it to the query string (we can't see this code here). The decodeURIComponent in your function is also not necessary.
function getPosName (URI) {
//var uri_dec = decodeURIComponent(uri);
var url = new URL(URI);
var posName = url.searchParams.get("posName");
return posName;
}
console.log("Using your current URI:", getPosName("http://exemple.com?posName=Content+&+Community+Manager+(H/F)"))
const encodedParam = encodeURIComponent("Content & Community Manager (H/F)")
console.log("Encoded Parameter:", encodedParam)
const wellFormedURI = `http://exemple.com?posName=${encodedParam}`
console.log("Well Formed URI:", wellFormedURI)
console.log("Using well formed URI:", getPosName(wellFormedURI))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With