Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

javascript - 检测HTTP或HTTPS,然后在JavaScript中强制使用HTTPS(Detect HTTP or HTTPS then force HTTPS in JavaScript)

Is there any way to detect HTTP or HTTPS and then force usage of HTTPS with JavaScript?(有没有办法检测HTTP或HTTPS,然后强制使用HTTPS与JavaScript?)

I have some codes for detecting the HTTP or HTTPS but I can't force it to use https: .(我有一些用于检测HTTP或HTTPS的代码,但我不能强迫它使用https: .) I'm using the window.location.protocol property to set whatever the site is to https: then refresh the page to hopefully reload a new https'ed URL loaded into the browser.(我正在使用window.location.protocol属性来设置站点到https:任何内容https:然后刷新页面以希望重新加载加载到浏览器中的新https'ed URL。) if (window.location.protocol != "https:") { window.location.protocol = "https:"; window.location.reload(); }   ask by Registered User translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this(试试这个)

if (location.protocol != 'https:') { location.href = 'https:' + window.location.href.substring(window.location.protocol.length); }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...