Before I go on, let me say that I've looked through a number of threads already and can't find an answer that works for me.
Basically, I've built a custom link shortener and I'm using URLSearchParams to pull the URL to be shortened and the custom slug from the URL search query as follows:
var e = window.location.search;
const urlParams = new URLSearchParams(e);
const url = urlParams.get("url");
const slug = urlParams.get("slug");
Where the format for a query is: ?url=https://google.com&slug=customslug
After the parameters are handled, the URL string is treated with trim()
to remove any whitespace. The final output is encoded with encodeURIComponent()
when the API I'm using (https://short.io) is called.
However, I want to be able to pass URLs with &
, like so: ?url=https://google.com/&testing&slug=customslug
. My ideal solution would simply treat any &
that isn't part of the &slug
parameter as a part of the URL contained within the &url
parameter. Currently, the &
character is ignored if it isn't attached to a valid parameter (url
or slug
).
I have tried encoding the query input using encodeURIComponent()
, but that results in a failure to pick up on either defined parameter. I have also tried splitting the input using split("&slug",1)
, but that results in an array and I cannot pass arrays to the Short.io API.
Any suggestions?