Onclick重定向到https

时间:2018-04-24 09:05:22

标签: javascript html https

我已经使用https证书更新了一个大型(ish)网站。由于来自Google的流量,我目前只将https应用于新页面(我计划逐步过渡 - 我不希望流量在重新编制索引时直线下降)。问题是每个页面都有一个登录表单。

有没有办法可以添加javascript(内联或其他方式),这样如果页面不是https,'onclick'(以下字段)会指向login.php的https版本?

type="password" name="login_password" id="navbar_password"
type="text" name="login_username" id="navbar_username"

感谢任何帮助。

亲切的问候,

S.C>

2 个答案:

答案 0 :(得分:0)

<强>已更新

您可以检查用户当前是否使用非https版本,并在输入获得焦点时根据需要重定向他,就像您提出的那样。

在HTML中

<input type="text" name="login_username" id="navbar_username" 
onfocus="checkRedirect();">

在JS中

function checkRedirect() {
  if (window.location.protocol != "https:") {
    // is not https version
    window.location.replace('https://your-https-domain.com/');
  }
}

答案 1 :(得分:0)

您有两种选择:

1)AJAX调用onclick动作

const xhr = new XMLHttpRequest();

xhr.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
        /* this.responseText */
    }
}

xhr.open("POST", "https://your.domain.com/login.php");
xhr.send({ username: "foo", password: "bar" });

More AJAX details

2)绝对表单操作URL

<form method="POST" action="https://your.domain.com/login.php">
...
</form>