Skip to content

Instantly share code, notes, and snippets.

@jerrybendy
Created June 27, 2019 09:10
Show Gist options
  • Save jerrybendy/3d8f4e194be55134ae0eee9af02d36f1 to your computer and use it in GitHub Desktop.
Save jerrybendy/3d8f4e194be55134ae0eee9af02d36f1 to your computer and use it in GitHub Desktop.
Force modify a URL to HTTPS if your website is under HTTPS
/**
* Force modify a URL to HTTPS if your website is under HTTPS
*
* @example resolveHttps('http://example.com')
* If you are under a HTTPS website, it'll return `https://example.com`,
* Else it'll return `http://example.com`
*
* @param {string} url
* @return {string}
*/
function resolveHttps(url) {
if (location.protocol === 'https:' && /^http:/i.test(url)) {
return 'https:' + url.substr(5)
}
return url
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment