How do I determine whether a page is secure via JavaScript?

Viewed 36188

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?

3 Answers

location.protocol should do it for you.

(as in:

if (location.protocol === 'https:') {
    // page is secure
}

)

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

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.

Related