What is the shortest way to secure a site with Javascript?
I am new to Code Golfing, and this is what I've come up with so far. I bet that this could be improved though:
Challenge:
Make a script which redirects
http://
tohttps://
with the least bytes of code
Attempts:
Original code (150ish BYTES!):
if (window.location.protocol != "https:") {
window.location.href =
"https:" + window.location.href.substring(window.location.protocol.length);
}
My code so far (68 bytes):
var l=location;':'==l.protocol[4]&&(l.href=l.href.replace(':','s:'))
Updated, thanks to @mypronounismonicareinstate (52 bytes):
l=location;l["protocol"][5]||(l["protocol"]="https")
Thanks to @arnauld (45 bytes):
(l=location)[p='protocol'][5]||(l[p]='https')
This works, thanks to @anderskaseorg (38 bytes):
with(location)protocol='https'+href[4]
However, this last answer would not be commonly used on a real site
Could this be minified more?
==
can be replaced by<
(because":" < "s"
). You might also be able to skip thevar
(because if the rest of the code doesn't use global variables, this will only help find bugs!). \$\endgroup\$location.protocol='https'
seemingly can be used instead of thel.href=...
magic. \$\endgroup\$(l=location)[p='protocol'][5]||(l[p]='https')
(45 bytes) \$\endgroup\$