有没有办法检测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) http://如果URL尚未包含协议(例如http://,https://或ftp://),我该如何添加?
例:
addhttp("google.com"); // http://google.com
addhttp("www.google.com"); // http://www.google.com
addhttp("google.com"); // http://google.com
addhttp("ftp://google.com"); // ftp://google.com
addhttp("https://google.com"); // https://google.com
addhttp("http://google.com"); // http://google.com
addhttp("rubbish"); // http://rubbish
Run Code Online (Sandbox Code Playgroud)