2010-12-01 24 views
1

我想使用JavaScript将http的每个实例更改为https。我怎样才能做到这一点?源代码是什么样的?如何在源代码中使用JavaScript将每个http更改为https?

+1

你为什么要这么做?只需在浏览器中关闭JS,就可以禁用通过JS更改任何内容。如果您希望强制您的站点/应用程序更改所有链接以使用SSL,那么您应该在服务器上的源代码中执行此操作,或者如果它位于Apache上,则可以使用.htaccess文件重定向。 – 2011-03-24 13:43:09

回答

-1

您可以http流量重定向到https在JavaScript中的东西,如:

<script type="text/javascript"> 
<!-- begin hide 
function httpsRedirect() 
{ 
var httpURL = window.location.hostname + window.location.pathname; 
var httpsURL = "https://" + httpURL; 
window.location = httpsURL; 
} 
httpsRedirect(); 
// end hide -->; 
</script> 
0

无论如何,我认为你是什么后,在标签,但它应该是很容易推断这个别人:

if (document.location.protocol === 'https:') { 
    $('a').each(function() { 
     var href = $(this).attr('href'); 
     if (href.indexOf('http:') > -1) { 
      href = href.replace('http:', 'https:'); 
      $(this).attr('href', href); 
     } 
    }); 
} 
0

您可以在https域页面中向servlet抛出https更改请求,服务器将重定向到https域页面。尝试在javascript中将域名从http更改为https将导致浏览器安全错误(因为跨域请求在所有现代浏览器中均被拒绝)。 我解决了同样的问题,如下所示。

function readyForSecure(loginID) 
{ 
    if (location.protocol == 'http:') { 
    // https change request 
     HTMLFormElement.prototype.submit.call(document.getElementById('login-box')); 
    } 
} 


<form id="login-box" action='xxxxxx' method="post" accept-charset="UTF-8"> 
    ... 
    <input type="button" value="Login" onfocus="readyForSecure(this.value)"/> 
</form> 

你可以参考这里。 refer page.