Prepend https to URL if necessary
Posted on October 10, 2021
This javascript function prepends the https://
chunk to a provided URL only if missing:
var prependHTTP = url => {
url = decodeURIComponent(url)
if (!/^(?:f|ht)tps?\:\/\//.test(url)) {
url = 'https://' + url
}
return url
}
This way, we can be sure that the final URL contains the https
protocol:
prependHTTP('https://www.google.com') //'https://www.google.com'
prependHTTP('www.google.com') //'https://www.google.com'