重定向网站重定向到HTTPS?

时间:2014-10-31 22:39:09

标签: javascript http cookies https web

我在尝试使用javascript将我的网站从HTTP重定向到HTTPS时遇到问题(我知道这不是最好的选择),并且它正在运行,但我想添加3次重试的限制,如果浏览器获胜切换到https,它将重定向到我的过期页面。而我正试图用饼干计算回顾。不幸的是,它似乎不起作用,甚至我重定向到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://gamingwiththecrew.com/outofdate.html");

if (loc.indexOf('http://')==0){
document.cookie="retry= + 1";
window.location.href = loc.replace('http://','https://');
}
</script>

2 个答案:

答案 0 :(得分:1)

您应该检查代码是否存在语法错误:

检查if(retry>3) [的语法。例外&#39; [&#39; 应该是&#39; {&#39;

您应该尝试window.retry全局存储变量,而不是使用&#39; Cookie&#39;,因为某些访问者可能会在浏览器中停用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>

希望有帮助吗?

答案 1 :(得分:0)

好的,谢谢stanley amos,但是当我尝试示例代码时,它仍然无法正常工作。代码似乎是&#34;挂&#34;在到loc.replace之前。 但我通过移动来解决它

if (loc.indexOf('http://')==0){
到了开头。 所以我的代码看起来像这样:

<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://gamingwiththecrew.com/outofdate.html");
}

</script>