返回列表

segmentfault 强制跳转 https

默认分类 2019/08/08 06:38

segmentfault 强制跳转 https的代码片段,随手记录下:

(function(currentUrl) {
  if (typeof URL != 'undefined') {
    // 测试环境
    if ('https://segmentfault.com' === '//localhost:3000') return

    var baseUrl = new URL('https://segmentfault.com');

    if (baseUrl.protocol != currentUrl.protocol || baseUrl.host != currentUrl.host) {
      window.location.href = baseUrl.protocol + '//' + baseUrl.host + currentUrl.pathname + currentUrl.search + currentUrl.hash;
    }
  }
})(window.location);


(function(currentUrl) {
  if (typeof URL != 'undefined') {
    var baseUrl = new URL('https://segmentfault.com');

    if (baseUrl.protocol != currentUrl.protocol ||
      baseUrl.host != currentUrl.host) {
      window.location.href = baseUrl.protocol + '//' + baseUrl.host +
        currentUrl.pathname + currentUrl.search + currentUrl.hash;
    }
  }
})(window.location);


;(function(currentUrl) {
  if (typeof URL != 'undefined') {
    var baseUrl = new URL('https://a.com');

    if (baseUrl.host === currentUrl.host && baseUrl.protocol != currentUrl.protocol) {
      window.location.href = baseUrl.protocol + '//' + baseUrl.host +
        currentUrl.pathname + currentUrl.search + currentUrl.hash;
    }
  }
})(window.location);


>> 留言评论