【问题标题】:Redirect Website to HTTPS with limited retry?通过有限的重试将网站重定向到 HTTPS?
【发布时间】: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


【解决方案1】:

你应该检查你的代码是否有语法错误:

检查if(retry&gt;3) [ 的语法。例外 '[' 应改为 '{'

您应该尝试window.retry 来全局存储变量而不是使用“cookie”,因为有些访问者可能会在他们的浏览器中禁用 cookie。

一个例子应该是:

<script language="JavaScript">

window.retry = 0;
var loc = window.location+'';
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://gamingwiththecrew.com/outofdate.html");
     if (loc.indexOf('http://')==0){
         window.retry +=1;
         window.location.href = loc.replace('http://','https://');
 }

</script>

希望有帮助吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多