How do I determine whether a page is secure via JavaScript?
JavascriptHttpsJavascript 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.