js强制性http的网站跳转到https

来源:网络 点击数: 发布时间:2021年01月04日
  1. 问题:使用js强制性http的网站跳转到https

  2. 解决:有两种方式

  3. 第一种是获取判断如果不是https就强制跳到http

  4. <script type="text/javascript">

  5. var targetProtocol = "https:";

  6. if (window.location.protocol != targetProtocol)

  7. window.location.href = targetProtocol +

  8. window.location.href.substring(window.location.protocol.length);

  9. </script>

  10. 第二种,同上,不过支持低版本的浏览器

  11. <script type="text/javascript">  

  12.     var url=window.location.href;  

  13.     url=url.replace("http:","https:")  

  14.     window.location.replace(url);

  15. </script>