检测HTTP或HTTPS,然后在JavaScript中强制使用HTTPS [英] Detect HTTP or HTTPS then force HTTPS in JavaScript

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

问题描述

有没有办法检测HTTP或HTTPS然后强制使用HTTPS与JavaScript?

Is there any way to detect HTTP or HTTPS and then force usage of HTTPS with JavaScript?

我有一些代码用于检测HTTP或HTTPS,但我可以' t强迫它使用 https:

I have some codes for detecting the HTTP or HTTPS but I can't force it to use https: .

我正在使用 window.location.protocol 属性设置网站的任何内容 https:然后刷新页面以希望重新加载加载到浏览器中的新https'ed URL。

I'm using the window.location.protocol property to set whatever the site is to https: then refresh the page to hopefully reload a new https'ed URL loaded into the browser.

if (window.location.protocol != "https:") {
   window.location.protocol = "https:";
   window.location.reload();
}


推荐答案

试试这个

if (location.protocol != 'https:')
{
 location.href = 'https:' + window.location.href.substring(window.location.protocol.length);
}

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

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