3
\$\begingroup\$

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:// to https:// 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?

\$\endgroup\$
9
  • \$\begingroup\$ I think you want the code-golf tag, and also check out the Sandbox. Good luck! \$\endgroup\$
    – user
    Commented Aug 8, 2020 at 15:25
  • \$\begingroup\$ I think the == can be replaced by < (because ":" < "s"). You might also be able to skip the var (because if the rest of the code doesn't use global variables, this will only help find bugs!). \$\endgroup\$ Commented Aug 8, 2020 at 15:43
  • \$\begingroup\$ I tested in Firefox and Chrome (for some reason I don't have any other browsers installed), and simply location.protocol='https' seemingly can be used instead of the l.href=... magic. \$\endgroup\$ Commented Aug 8, 2020 at 15:49
  • 1
    \$\begingroup\$ @mypronounismonicareinstate which leads to (l=location)[p='protocol'][5]||(l[p]='https') (45 bytes) \$\endgroup\$
    – Arnauld
    Commented Aug 8, 2020 at 15:59
  • 2
    \$\begingroup\$ Just to make this perfectly clear: this site is about recreational programming competitions, where only the winning criterion matters (the code size in that case). If you are really looking for a clean, reliable, cross-platform compatible way to perform this task (or any other task), you definitely shouldn't be asking here. \$\endgroup\$
    – Arnauld
    Commented Aug 10, 2020 at 5:35

1 Answer 1

7
\$\begingroup\$

39 38 bytes

with(location)protocol='https'+href[4]

How it works

  • If location.href starts with http:, this assigns location.protocol = 'https:'.
  • If location.href starts with https:, this assigns location.protocol = 'httpss'. According to the specification, this should have no effect because httpss is neither http nor https. Firefox obeys this specification. Chrome seems to try to find an external app to open the httpss scheme (bug).
\$\endgroup\$
5
  • 3
    \$\begingroup\$ Use of with is not recommended for websites. Alternative (a byte longer): (l=location).protocol='https'+l.href[4]. \$\endgroup\$
    – vrintle
    Commented Aug 9, 2020 at 5:52
  • 14
    \$\begingroup\$ @RahulVerma This is code-golf; anything that works is fair game. If you’re looking for recommended advice, stay far, far away from this entire site. \$\endgroup\$ Commented Aug 9, 2020 at 5:56
  • 2
    \$\begingroup\$ Despite of the fact that this site isn't suitable for recommended solution, with has bugs and compatibility issues, due to which it may fail in some browsers. \$\endgroup\$
    – vrintle
    Commented Aug 9, 2020 at 6:56
  • 2
    \$\begingroup\$ @RahulVerma Read your link more carefully: with does not have bugs and compatibility issues, it may be the source of bugs and compatibility issues. If you read farther down, the described issues are in code you might write using with that’s confusing to humans or that might break unintuitively as JS evolves. These are valid criticisms of with—you should never use it—but, again, this site has nothing to do with “should”. The with statement itself behaves correctly as defined by the specification, and will not be changed; my use of with is not affected by these issues. \$\endgroup\$ Commented Aug 9, 2020 at 7:22
  • \$\begingroup\$ This seems ok, but it says cannot open page \$\endgroup\$
    – derder56
    Commented Aug 9, 2020 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.