Javascript replacing /, str.replace not working - javascript

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.

Related

Split URL into params

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('-');

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'));

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 strings : Appending django urls

I'm trying to create a simple url builder. For example in javascript I have:
var url = document.URL;
If I wanted to append something to the url I could just simply type its pattern, but in Django we can use something like this:
url = url + "{% url 'object_view' %}";
alert(url);
The problem is where the document.URL and Django URL creates a pattern like this:
http://localhost:8000//objects/view/
I've tried looking at Javascript string manipulation such as trim() and replace(), but both doesn't have the manipulation to just drop the single slash in the string from document.URL.
If I replace all () it may get affected if for example my document.URL is something like: http://localhost:8000/something/something2/
Any suggestions?
In your case, you can replace // if not preceded by :
"http://localhost:8000//objects/view/".replace( /(?<!:)\/\//g, "/" )
Figured that I should be able to drop the last / from document.URL by using slice()
var url = document.URL;
url = url.slice(0, -1);
alert(url);
Output:
http://localhost:8000
then append the remaining:
url = url + "{% url 'routes_view' %}".toString();
Output:
http://localhost:8000/something/something2/

How to replace / except in http://

I have a string and I need to replace %2f with /, except in http://.
Example:
var str = "http://locahost%2f";
str = str.replace(/%2f/g, "/");
Somehow I got str to output http:/locahost/.
Thanks in advance!
What should be used
You should use decodeURIComponent or decodeURI functions to decode encoded strings like yours. For example, decodeURIComponent("http://locahost%2f") will return http://localhost/.
That being said, decodeURIComponent should be used to decode components of a URI and not a full URI like http://locahost/.
You should look at this answer, it explains what is the difference between decodeURIComponent and decodeURI and when each should be used.
A workaround
As the first / are not encoded, it makes it difficult to find a Javascript functions doing exactly what you want. A working solution to decode your %2f after http:// would be to use String.prototype.split.
Working example:
var encoded = "http://localhost%2f";
var encodedArray = str.split("://");
var decoded = encodedArray[0] + "://" + encodedArray[1].replace(/%2f/g, "/");
This should work:
str = str.replace("%2f", "/");

Categories

Resources