html - 如果只更改当前 URL 的协议(protocol),相对 href 链接看起来如何?

标签 html http https hyperlink protocols

假设我在

http://sub.example.org/path/index.htm

如何

<a href="">Link</a>

行看看它是否应该做的只是将协议(protocol)更改为 https:

https://sub.example.org/path/index.htm

不需要知道页面所在的当前 URL (sub.example.org/path/index.htm),所以是相对的?

示例用例:提供“从现在开始使用安全连接 (HTTPS/TLS)!”所有页面上的链接。

最佳答案

您无法使用相对 URI 实现此目的。相对 URI 总是从某个点开始,然后将一切更改到该点的右侧。

您可以使用服务器端代码或 JavaScript 计算 URL:

location = location.toString().replace(/^http:/, "https:");

关于html - 如果只更改当前 URL 的协议(protocol),相对 href 链接看起来如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23580426/

相关文章:

html - inuit.css 窗口调整大小时图像高度不均匀

html - 为 HTML5 Canvas 上的透明圆弧修改渐变的透明度

ssl - Nginx 基本身份验证适用于 http 但不适用于 https

.htaccess - 将域重定向到子域 + 使其成为 https

r - httr::POST 中查询和正文之间的区别

powershell - CouchDB SSL CRT 到 PEM 文件

html - 具有像素尺寸的 div 在不应该时进行换行

html - 将简单文本插入到 iframe 中?不是 .htm 文件

嵌套对象的 RESTful 重新排序

java.net.UnknownHostException @ Android(简单的 HttpResponse 代码)