js判断当前域名是http还是https的方法
墨初 Web前端 1491阅读
想要通过js脚本来判断当前的网址的HTTP协议是HTTP还是HTTPS,可以使用js中document对象中的location的子对象,下面是具体的使用方法。
js判断HTTP还是HTTPS
location的子对象protocol可以获取网址中的协议。
例:
console.log(window.location.protocol); // 返回 https: 或 http:
例:
var Protocol = window.location.protocol.split(':')[0];
// https://www.73so.com
//获取当前协议,并且分割字符串,得到http或者https
if (Protocol === 'https') {
    console.log("当前协议是https");
}
else {
    console.log("当前协议是http");
}以上就是js获取当前网址中HTTP协议类型的方法。
