This question already has an answer here:
这个问题在这里已有答案:
- How can I use JavaScript on the client side to detect if the page was encrypted? 2 answers
- 如何在客户端使用JavaScript来检测页面是否已加密? 2个答案
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通过http或https访问该页面。是否有某种isSecure()方法,或者我应该以某种方式解析它的URL?
3 个解决方案
#1
87
location.protocol
should do it for you.
location.protocol应该为你做。
(as in:
(如:
if (location.protocol === 'https:') {
// page is secure
}
)
)
#2
9
You should be able to check document.location.protocol
to see if it's "http:" or "https:"
您应该能够检查document.location.protocol以查看它是“http:”还是“https:”
#3
2
While location.protocol should do it for you as Peter Stone mentioned, but you shouldn't rely on Javascript for any true security, etc.
虽然location.protocol应该像Peter Stone所说的那样为你做,但你不应该依赖Javascript来获得任何真正的安全性等。
I think the value with be "https:" for location.protocol if you are on SSL.
如果您使用SSL,我认为location.protocol的值为“https:”。
#1
87
location.protocol
should do it for you.
location.protocol应该为你做。
(as in:
(如:
if (location.protocol === 'https:') {
// page is secure
}
)
)
#2
9
You should be able to check document.location.protocol
to see if it's "http:" or "https:"
您应该能够检查document.location.protocol以查看它是“http:”还是“https:”
#3
2
While location.protocol should do it for you as Peter Stone mentioned, but you shouldn't rely on Javascript for any true security, etc.
虽然location.protocol应该像Peter Stone所说的那样为你做,但你不应该依赖Javascript来获得任何真正的安全性等。
I think the value with be "https:" for location.protocol if you are on SSL.
如果您使用SSL,我认为location.protocol的值为“https:”。