强制转化成https

(function(){
  if (location.protocol !== 'https:'){
	var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
    if(!reg.test(location.host.split(":")[0]) && location.host.indexOf('localhost') === -1){
      location.replace('https://' + location.href.split('//')[1]);
    }
  }else{
    let head = document.getElementsByTagName("head");
    let meta = document.createElement("meta");
    meta.content = "upgrade-insecure-requests";
    meta.httpEquiv = "Content-Security-Policy";
    head[0].appendChild(meta);
  }
})()
posted on 2021-11-03 15:18  忆小样  阅读(34)  评论(0编辑  收藏  举报