How do I determine whether a page is secure via JavaScript? [duplicate]
-
03-07-2019 - |
Question
This question already has an answer here:
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?
Solution
location.protocol
should do it for you.
(as in:
if (location.protocol === 'https:') {
// page is secure
}
)
OTHER TIPS
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow