使用JavaScript检测HTTPS [英] Detect HTTPS with JavaScript

查看:195
本文介绍了使用JavaScript检测HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到如果我在HTTP或HTTPS环境中如何使用JavaScript检测。

I am trying to find how can I detect with JavaScript if I am in a HTTP or HTTPS environment.

我正在调用Ajax请求,所以如果我在HTTPS并调用HTTP Ajax然后我得到302暂时移动。

I am calling an Ajax request so if I am in HTTPS and call HTTP Ajax then I get a 302 Moved Temporarily.

我想得到当前的 window.location.href 并进行字符串操作。

I was thinking of getting the current window.location.href and do a string manipulation.

使用JavaScript检测HTTPS的最佳方法是什么?

What is the best way of detecting HTTPS using JavaScript?

推荐答案

您可以使用非标准

window.location.protocol 

在Firefox中: MDC文档

In Firefox: MDC documentation

在IE中,似乎是

 document.location.protocol

MSDN文档

我找不到关于这个的可靠信息在其他浏览器上运行,但我希望它们符合 document.location.protocol 的准标准。

I can't find reliable info on how this behaves on other browsers, but I expect they adhere to the quasi-standard of document.location.protocol.

也许 jQuery url插件排序这不用处理跨浏览器的差异 - 我自己从未使用它,但看起来很有希望:

Maybe the jQuery url plugin sorts this out without having to deal with cross-browser differences - I've never used it myself, but it looks promising:

jQuery.url.attr("protocol");

这篇关于使用JavaScript检测HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆