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

查看:40
本文介绍了使用 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 Moved Temporively.

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 中,它似乎是

In IE, it seems to be

 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天全站免登陆