簡體   English   中英

如何在源代碼中使用JavaScript將每個http更改為https?

[英]How can I change every http to https using JavaScript in source code?

我想使用JavaScript將http的每個實例更改為https。 我怎樣才能做到這一點? 源代碼是什么樣的?

無論如何,我假設您追求的是標簽中的內容,但應該很容易將其推斷給其他人:

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);
        }
    });
}

您可以在http域頁面中向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>

您可以在這里參考。 參考頁面。

您可以使用類似以下內容的JavaScript將http流量重定向到https:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM