I want to know if the page is being accessed via http or https using JavaScript. Is there some sort of isSecure() method, or should I just parse it out of the URL somehow?
+4
A:
location.protocol
should do it for you.
(as in:
if (location.protocol === 'https:') {
// page is secure
}
)
Peter Stone
2009-01-05 23:00:33
+3
A:
You should be able to check document.location.protocol
to see if it's "http:" or "https:"
Marc Novakowski
2009-01-05 23:00:47
+1
A:
While location.protocol should do it for you as Peter Stone mentioned, but you shouldn't rely on Javascript for any true security, etc.
I think the value with be "https:" for location.protocol if you are on SSL.
Jason Jackson
2009-01-05 23:03:26
Yeah, if you need SSL for security, check server-side (Apache sets the HTTPS environment variable if SSL is in use).If you just need to know which it is (i.e., use secure Google Analytics to avoid "partial security" warnings), this will be okay.
Peter Stone
2009-01-05 23:13:40
I just need to know which it is to avoid the "non-secure elements warnings in IE". It's a stopgap measure until our next release.
braveterry
2009-01-05 23:27:10