【发布时间】:2020-06-26 14:15:25
【问题描述】:
大家好,stackoverflowers!我在使用 javascript 将我的网站从 HTTP 重定向到 HTTPS 时遇到了一些麻烦。在以下示例中,最终目标是尝试从 HTTP 重定向到 HTTPS,最多尝试 3 次。一旦超过此阈值,网站将重定向到警告页面。但是,当前代码无法正确重定向。
<script language="JavaScript">
var loc = window.location+'';
if (retry>=3)[
alert('We are sorry, but your client does NOT support SSL(Https) protocol.')
alert("This Website Can Not Be Loaded, Your Browser Is Out Of Date!!!")
window.location.replace("https://example.com/outofdate.html");
if (loc.indexOf('http://')==0){
document.cookie="retry= + 1";
window.location.href = loc.replace('http://','https://');
}
</script>
编辑:作为参考,现在使用以下代码解决了这个问题:
<script language="JavaScript">
window.retry = 0;
var loc = window.location+'';
if (loc.indexOf('http://')==0){
window.retry +=1;
window.location.href = loc.replace('http://','https://');
}
if(window.retry>=3){
alert('We are sorry, but your client does NOT support SSL(Https)protocol.');
alert("This Website Can Not Be Loaded, Your Browser Is Out Of Date!!!");
window.location.replace("https://example.com/outofdate.html");
}
</script>
【问题讨论】:
-
检查你的代码是否有语法错误,你的第一个如果要结束的话在哪里?您也可以使用 window.location.protocol,并且需要将 cookie 值
retry增加 1,而不是将其设置为+ 1。编辑:您尝试“support”哪些客户?几乎所有能够呈现网站的浏览器都支持 https。
标签: javascript http cookies https web