Javascript 是否将网站重定向到HTTPS并进行有限重试?

Javascript 是否将网站重定向到HTTPS并进行有限重试?,javascript,http,cookies,https,web,Javascript,Http,Cookies,Https,Web,嗨,各位飞越者!我在使用javascript将我的网站从HTTP重定向到HTTPS时遇到了一些问题。在下面的示例中,最终目标是尝试从HTTP重定向到HTTPS,最多尝试三次。一旦超过此阈值,网站将重定向到警告页面。但是,当前代码没有正确重定向 <script language="JavaScript"> var loc = window.location+''; if (retry>=3)[ alert('We are sorry, but your cli

嗨,各位飞越者!我在使用javascript将我的网站从HTTP重定向到HTTPS时遇到了一些问题。在下面的示例中,最终目标是尝试从HTTP重定向到HTTPS,最多尝试三次。一旦超过此阈值,网站将重定向到警告页面。但是,当前代码没有正确重定向

<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>

var loc=窗口位置+“”;
如果(重试>=3)[
警报('很抱歉,您的客户端不支持SSL(Https)协议。')
警报(“无法加载此网站,您的浏览器已过期!!!”)
window.location.replace(“https://example.com/outofdate.html");
if(loc.indexOf('http://')==0){
document.cookie=“重试=+1”;
window.location.href=loc.replace('http://','https://');
}
编辑:作为参考,此问题现在通过以下代码解决:

<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.retry=0;
var loc=窗口位置+“”;
if(loc.indexOf('http://')==0){
window.retry+=1;
window.location.href=loc.replace('http://','https://');
}
如果(窗口重试>=3){
警报('很抱歉,您的客户端不支持SSL(Https)协议');
警告(“无法加载此网站,您的浏览器已过期!!!”;
window.location.replace(“https://example.com/outofdate.html");
}

您应该检查代码的语法错误:

检查
if(retry>3)[
的语法。异常'['应改为'{'

您应该尝试
窗口。重试
以全局存储变量,而不是使用“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>

window.retry=0;
var loc=窗口位置+“”;
如果(窗口重试>=3){
警报('很抱歉,您的客户端不支持SSL(Https)协议');
警告(“无法加载此网站,您的浏览器已过期!!!”;
window.location.replace(“https://gamingwiththecrew.com/outofdate.html");
if(loc.indexOf('http://')==0){
window.retry+=1;
window.location.href=loc.replace('http://','https://');
}

希望这有帮助?

检查您的代码是否存在语法错误,如果打算结束,您的第一个错误在哪里?您还可以使用window.location.protocol,并且您需要将cookie值
retry
增加1,而不是将其设置为
+1
。编辑:您尝试“支持”哪些客户端?几乎所有能够呈现网站的浏览器都支持https。