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

JavascriptHttps

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

location.protocol should do it for you.

(as in:

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

)

Solution 2 - Javascript

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

Solution 3 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbraveterryView Question on Stackoverflow
Solution 1 - JavascriptPeter StoneView Answer on Stackoverflow
Solution 2 - JavascriptMarc NovakowskiView Answer on Stackoverflow
Solution 3 - JavascriptJason JacksonView Answer on Stackoverflow