当前位置:   article > 正文

JavaScript 判断当前协议是http还是https_js 判断http

js 判断http

JavaScript 的document对象中有一个location的子对象,其包括是属性如下:

 

  1. document.location.host   //表示当前域名 + 端口号
  2. document.location.hostname  //表示域名
  3. document.location.href   //表示完整的URL
  4. document.location.port   //表示端口号
  5. document.location.protocol   //表示当前的网络协议

   所以通过上面第五条就能判断当前的网络协议了,具体判断如下:

 

 

  1. var protocolStr = document.location.protocol;
  2. if(protocolStr == "http:")
  3. {
  4. console.log("protocol = " + protocolStr);
  5. }
  6. else if(protocolStr == "https:")
  7. {
  8. console.log("protocol = " + protocolStr);
  9. }
  10. else
  11. {
  12. console.log("other protocol");
  13. }

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/731757
推荐阅读
相关标签
  

闽ICP备14008679号