gpt4 book ai didi

javascript - 如何制作每个页面加载运行一次的 ssl 转发脚本

转载 作者:行者123 更新时间:2023-11-28 19:28:46 28 4
gpt4 key购买 nike

我在使用 javascript 脚本时遇到了一些问题,该脚本会自动让您转而使用 ssl。例如,

http://www.example.com/link becomes https://www.example.com/link

但我的问题是连续加载脚本,但我希望它在加载完毕后停止。它会不断重新加载,这使得点击页面上的链接非常烦人和困难。

这是我当前的脚本

window.location = "https://" + window.location.hostname + window.location.pathname + window.location.search;

最佳答案

你需要这个:

if(window.location.protocol == 'http:') {
window.location = "https://" + window.location.hostname + window.location.pathname + window.location.search;
}

...甚至更好:

if(window.location.protocol == 'http:') {
window.location.replace(window.location.toString().replace(/^http:/, 'https:'));
}

第二个变体更好,因为:

  • 网址可能很复杂,我们会妥善处理
  • 由于使用 window.location.replace() 而不是直接将字符串分配给 window.location,之前的 URL 将从历史记录中删除,并且当用户点击 '返回'按钮,他会跳转到原始页面,而不是带有“http:”协议(protocol)的页面:http://www.w3schools.com/jsref/met_loc_replace.asp

但最好在服务器端实现它。

关于javascript - 如何制作每个页面加载运行一次的 ssl 转发脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27300666/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com