Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the URL parameter I want to retrieve not fully displayed?

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?

like image 501
B4b4j1 Avatar asked May 22 '19 00:05

B4b4j1


People also ask

How do I find the parameter of a URL?

For getting the URL parameters, there are 2 ways: By using the URLSearchParams Object. By using Separating and accessing each parameter pair.

How do URL parameters work?

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.

How do I pass URL URL?

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.

How do I hide information in a URL?

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.


1 Answers

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))
like image 145
BlueWater86 Avatar answered Oct 23 '22 21:10

BlueWater86