如何在源代码中使用 JavaScript 将每个 http 更改为 https?

发布于 2024-10-05 16:01:12 字数 60 浏览 1 评论 0原文

我想使用 JavaScript 将每个 http 实例更改为 https。我该怎么做?源代码会是什么样子?

I would like to change every instance of http to https using JavaScript. How can I do this? what would the source code look like?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离鸿 2024-10-12 16:01:12

不管怎样,我假设你所追求的是标签,但应该很容易将其推断到其他人:

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

Anyway, I assume what you are after is in tags, but it should be easy to extrapolate this to others:

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);
        }
    });
}
隐诗 2024-10-12 16:01:12

您可以向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>

你可以参考这里。 参考页面。

You can throw https change request to servlet in http domain page, and server will redirect to https domain page. Trying to change domain from http to https in javascript will cause browser security error(as cross domain requests are denied in all modern browsers).
I solved the same problem as shown below.

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>

you can refer here. refer page.

吻泪 2024-10-12 16:01:12

您可以在 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>

You can redirect http traffic to https in JavaScript with something like:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文