javascript convert hostname URL into full URL

138 views Asked by At

I want to convert a domain name url (e.g : discord.com, google.com into a full URL (e.g : https://discord.com/, https://www.google.com/).
The problem is that I cannot know in advance if it will be http or https.

I can try to do something like this -

try {
    url = 'https://' + url;
} catch {
    url = 'http://' + url;
}

but something inside me tells me it's not the correct way to do it (also it does not work)

1

There are 1 answers

2
Dedbot On

HTTPS is HTTP but more secure, usually HTTPS sites will upgrade your connection to HTTPS automatically if they support it. The most simple way to convert hostnames is to add HTTP:// and the website will do the rest.