Created
June 27, 2019 09:10
-
-
Save jerrybendy/3d8f4e194be55134ae0eee9af02d36f1 to your computer and use it in GitHub Desktop.
Force modify a URL to HTTPS if your website is under HTTPS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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