views:

393

answers:

3

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
+3  A: 

You should be able to check document.location.protocol to see if it's "http:" or "https:"

Marc Novakowski
+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
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
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