Split URL into params - javascript

I'm having trouble splitting the url into the following format for my URL.
Url = ?Point%20Cook-VIC-3030
desired format: Point Cook, VIC, 3030
So far I've tried the below to get rid of the "-" but I'm not sure how to get rid of the "?" and "%" and "20"
const url = "?Point%20Cook-VIC-3030"
const queryParams = url.split("-")
I tried using URLSearchParams but realized it doesn't have browser support for older browsers
which is a bummer

If your URL is encoded (due to space character) you need to properly decode it.
const url = '?Point%20Cook-VIC-3030';
// Decode URL and remove initial '?'
const decodedUrl = decodeURIComponent(url).substring(1);
// Split parameters
const paramerters = decodedUrl.split('-');

Related

Extracting certain values from a large URL string in javascript?

I have a big URL hash that is given in string form and need to extract each part of it:
type=recovery&access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9eyJhdWQiOiJhdXRoZW50aWNhdGV&expires_in=3600&refresh_token=sYlurmTtfrAhyHl39Oqwww&token_type=bearer&type=recovery
I have tried substr() but it isn't reliable because each item may have a differing amount of characters each time.
What's the best way to extract type, access_token, expires_in, refresh_token, token_type reliably?
Please use URLSearchParams
var urlSearchParams = new URLSearchParams("type=recovery&access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9eyJhdWQiOiJhdXRoZW50aWNhdGV&expires_in=3600&refresh_token=sYlurmTtfrAhyHl39Oqwww&token_type=bearer&type=recovery")
const params = Object.fromEntries(urlSearchParams.entries());
console.log(params)
You could use URL:
// Adding a dummy domain so the string is a valid url
let x = new URL('http://dummydomain.tpl/dummyfile.ext?' +
'type=recovery&access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9eyJhdWQiOiJhdXRoZW50aWNhdGV&expires_in=3600&refresh_token=sYlurmTtfrAhyHl39Oqwww&token_type=bearer&type=recovery');
x.searchParams.get("refresh_token")

Firefox converts URI to URL: How to force it to display URI

I want to generate an URL in which the whitespace is encoded to "%20", but the Browser automatically converts "%20" to whitespace. It's just a problem with Firefox and "%20"/whitespace. The comma "%2C"/, is not converted...
Is it possible to force firefox to show the URI and not converted URL?
The URL in the Browser should look like this (with %20 instead of whitespace)
But Browser automatically converts it to this (with whitespace)
have a try with the second part of this code
const urlString = `https://www.sample.com/events?area=frankfurt am main`
let url = new URL(urlString);
console.log(url.toString()); // your %20
url = new URL(urlString.replace(/ /g,"+"));
console.log(url.toString()); // + instead of %20
Try this
console.log(encodeURI('domain.com?q=something text'));

EncodedURIComponent getting encoded again after using EncodeURI

Im trying to endcode a URL. One of the param value in the URL has & in it and hence we are using encodeURIComponent to encode it. After that, we also need to encode the whole URL, and the encoded values are getting encoded again. What should we do in this case?
Value to be encoded, encodeURICOmponent('A & B')
encodedvalue = A%20%26%20B
Query param = {"filter":{"ab":{"$like":"%A%20%26%20B%"}}}
encodeURI(param)
finalURL = %7B%22filter%22%3A%7B%22e2%22%3A%7B%22%24like%22%3A%22%25A%2520%2526%2520B%25%22%7D%7D%7D
Only apply the encoding once.
const value = 'A & B';
const param = JSON.stringify({"filter":{"ab":{"$like":value}}});
const url = encodeURI(param);
console.log(url);

Using split to get only the URL without query string in javascript

I want to split the website name and get only URL without the query string
example:
www.xyz.com/.php?id=1
the URL can be of any length so I want to split to get the URL to only
xyz.com
able to split the URL and getting xyz.com/php?id=1
but how do I end the split and get only xyz.com
var domain2 = document.getElementById("domain_id").value.split("w.")[1];
You can use:
new URL()
for example -
var urlData = new URL("http://www.example.org/.php?id=1")
and than
urlData.host
which will only return the hostname
You can use a simple regex with match to capture the host in the way you want:
var url = 'www.xyz.com/.php?id=1';
var host = url.match(/www.(.*)\//)[1];
console.log(host)
Just adding it to other, you can also use this regex expression to capture everything up until the query string "?" like so;
This will also work if you want to grab any sub pages from url before the query string
var exp = new RegExp('^.*(?=([\?]))');
var url = exp.exec("www.xyz.com/.php?id=1");
var host = url[0];
console.log(host);

Javascript replacing /, str.replace not working

I'm having a really stupid issue where javascript is replacing every '/' with '%2F' in a url. Here is what i have now:
var url;
url = $(this).val();
url = str.replace('%2F', '/');
window.location.href = $(this).val();
What have I done wrong here?
You need to decode the url to convert the special characters back to what they should be (like changing %2F back to /). To do this, you can use decodeURI:
url = $(this).val();
url = decodeURI(url);
However, sometimes spaces get replaced by + instead of %20. So, to handle these cases, you must replace all + with %20 before decoding your url.
url = $(this).val();
url = url.replace('+', '%20');
url = decodeURI(url);
And now url is the decoded version of the url.

Categories

Resources