Reg*_*ser 278 javascript https window.location
有没有办法检测HTTP或HTTPS,然后强制使用HTTPS与JavaScript?
我有一些代码用于检测HTTP或HTTPS,但我不能强制它使用https:.
我正在使用window.location.protocol属性来设置站点的任何内容,https:然后刷新页面以希望重新加载加载到浏览器中的新https'ed URL.
if (window.location.protocol != "https:") {
window.location.protocol = "https:";
window.location.reload();
}
Run Code Online (Sandbox Code Playgroud)
Sou*_*mya 456
试试这个
if (location.protocol != 'https:')
{
location.href = 'https:' + window.location.href.substring(window.location.protocol.length);
}
Run Code Online (Sandbox Code Playgroud)
sam*_*sam 51
设置location.protocol导航到新URL.无需解析/切片.
if (location.protocol !== "https:") {
location.protocol = "https:";
}
Run Code Online (Sandbox Code Playgroud)
Firefox 49有一个可以工作的错误,https但https:没有.据说在Firefox 54中修复.
b1_*_*b1_ 20
这不是一个好主意,因为您只是临时将用户重定向到https,浏览器不保存此重定向.
您描述了web-server(apache,nginx等)http 301,http 302的任务
kei*_*rog 15
这个怎么样?
if (window.location.protocol !== 'https:') {
window.location = 'https://' + window.location.hostname + window.location.pathname + window.location.hash;
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,您可以在服务器端执行此操作.
小智 13
if (location.protocol == 'http:')
location.href = location.href.replace(/^http:/, 'https:')
Run Code Online (Sandbox Code Playgroud)
您应该检查一下:https : //developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/upgrade-insecure-requests
将此元标记添加到您的 index.html 内head
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
Run Code Online (Sandbox Code Playgroud)
希望它有所帮助。
| 归档时间: |
|
| 查看次数: |
227220 次 |
| 最近记录: |