如何在客户端使用JavaScript来检测页面是否已加密?

时间:2021-11-25 06:24:01

Is it possible to detect, on the client side, whether the user is using an encrypted page or not?

是否可以在客户端检测用户是否使用加密页面?

Put another way -- I want to know if the URL of the current page starts with http or https.

换句话说 - 我想知道当前页面的URL是以http还是https开头。

2 个解决方案

#1


72  

Use window.location.protocol to check if it is https:

使用window.location.protocol检查它是否为https:

function isSecure()
{
   return window.location.protocol == 'https:';
}

Alternatively you can omit specifying "window" if you don't have a locally scoped location.

或者,如果您没有本地范围的位置,则可以省略指定“窗口”。

function isSecure()
{
   return location.protocol == 'https:';
}

#2


11  

As google analytics taught me:

谷歌分析师告诉我:

if ("https:" == document.location.protocol) {
    /* secure */
} else {
    /* unsecure */
}

#1


72  

Use window.location.protocol to check if it is https:

使用window.location.protocol检查它是否为https:

function isSecure()
{
   return window.location.protocol == 'https:';
}

Alternatively you can omit specifying "window" if you don't have a locally scoped location.

或者,如果您没有本地范围的位置,则可以省略指定“窗口”。

function isSecure()
{
   return location.protocol == 'https:';
}

#2


11  

As google analytics taught me:

谷歌分析师告诉我:

if ("https:" == document.location.protocol) {
    /* secure */
} else {
    /* unsecure */
}