时间: 2020-11-25|40次围观|0 条评论

用‘http://example.com:8000/pathname?search=test#hash’为例:

let queryUrl = function (url = window.location.href) {
  let _url = document.createElement('a');
  _url.href = url;
  return {
    protocol :_url.protocol,//'http'
    host :_url.host,//'example.com:8000'
    hostname :_url.hostname,//'example.com'
    port :_url.port ? _url.port :'80',//'8000',
    pathname : _url.pathname,//'/pathname/'
    search :_url.search,//'?search=test'
    hash :_url.hash,//‘#hash’
  }        
}

调用方法queryUrl,可以获取url中的信息。

若参数url为空,则默认去当前浏览器的url作为参数。

转载于:https://www.cnblogs.com/xiaoqd/p/9072078.html

原文链接:https://blog.csdn.net/weixin_30342827/article/details/99956247

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《解析url中的信息
   

还没有人抢沙发呢~