首页 > 解决方案 > 如果网站不能使用“https://”,我如何在 Javascript 中转发我的页面?

问题描述

所以我有一个按钮,可以将页面转发到类似 example.tk 的网站,但它一直认为我只想转到它所在的网站文件夹中的 ./example.tk。

我已经尝试过 window.location、window.location.replace、window.location.href 和 self.location。

myButton.addEventListener('click', function() {
    window.location.href = "example.tk";
});

我希望网站从它的 url 更改为 example.tk,如果 example.tk 没有经过认证的 https 标签,我该怎么做?

标签: javascripthtmlurlhttpsforward

解决方案


我希望网站从它的 url 更改为 example.tk,如果 example.tk 没有经过认证的 https 标签,我该怎么做?

不太清楚你的意思是什么。如果您的意思是https://要从资源链接到http://资源,请明确:

window.location.href = "http://example.tk";
// ---------------------^^^^^^^

http:如果您的意思是,您想使用与当前页面(或)相同的协议https,您可以使用协议相对 URL

window.location.href = "//example.tk";
// ---------------------^^

这将使用当前页面的任何协议。上http://example.com,它将链接到http://example.tk。上https://example.com,它将链接到https://example.tk

将鼠标悬停在此片段中的链接以查看它的实际效果(无需单击):

<a href="//example.tk">hover this link</a>


推荐阅读