自学内容网 自学内容网

如何用JS校验HTTP和HTTPS地址

        在日常开发过程中,我们有时候对某些应用功能进行封装,但是在请求接口又不能写死,这个时候我们需要对他进行多方面考虑。

如何验证请求地址是HTTP还是HTTPS

方法一:

function getBaseUrl (string) {
  let url;
  try {
    url = new URL(string);
  } catch (_) {
    return false;
  }
  return url.protocol === "https:";
}


// getBaseUrl ("<https://baidu.com>"); // true
// getBaseUrl ("<http://baidu.com>"); // false

方法二:

function getBaseUrl () {
  var ishttps = 'https:' == document.location.protocol ? true: false;
  return ishttps ;
}


原文地址:https://blog.csdn.net/qq_43474235/article/details/138046131

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!