用js轻松判断当前网络协议是http还是https
答案:1 悬赏:80
解决时间 2021-02-17 17:08
- 提问者网友:自食苦果
- 2021-02-16 18:20
用js轻松判断当前网络协议是http还是https
最佳答案
- 二级知识专家网友:北方的南先生
- 2021-02-16 19:51
JavaScript 的document对象中有一个location的子对象,其包括是属性如下:
document.location.host //表示当前域名 + 端口号
document.location.hostname//表示域名
document.location.href //表示完整的URL
document.location.port //表示端口号
document.location.protocol //表示当前的网络协议
所以通过上面第五条就能判断当前的网络协议了,具体判断如下:
[javascript] view plain copy
var protocolStr = document.location.protocol;
if(protocolStr == "http")
{
console.log("protocol = " + protocolStr);
}
else if(protocolStr == "https")
{
console.log("protocol = " + protocolStr);
}
else
{
console.log("other protocol");
}
document.location.host //表示当前域名 + 端口号
document.location.hostname//表示域名
document.location.href //表示完整的URL
document.location.port //表示端口号
document.location.protocol //表示当前的网络协议
所以通过上面第五条就能判断当前的网络协议了,具体判断如下:
[javascript] view plain copy
var protocolStr = document.location.protocol;
if(protocolStr == "http")
{
console.log("protocol = " + protocolStr);
}
else if(protocolStr == "https")
{
console.log("protocol = " + protocolStr);
}
else
{
console.log("other protocol");
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
• 手机登qq时,显示手机磁盘不足,清理后重新登 |
• 刺客的套装怎么选啊? |