zoukankan      html  css  js  c++  java
  • 解析URL的主机名

    字符串 http://www.abc.com/a/b?name=ss,返回字符串www.abc.com

    function parseURL(url) {
     var a =  document.createElement('a');
     a.href = url;
     return {
     source: url,
     protocol: a.protocol.replace(':',''),
     host: a.hostname,
     port: a.port,
     query: a.search,
     params: (function(){
         var ret = {},
             seg = a.search.replace(/^?/,'').split('&'),
             len = seg.length, i = 0, s;
         for (;i<len;i++) {
             if (!seg[i]) { continue; }
             s = seg[i].split('=');
             ret[s[0]] = s[1];
         }
         return ret;
     })(),
     hash: a.hash.replace('#',''),
     path: a.pathname
    
     };
    }
    
    var myURL = parseURL('http://www.abc.com:8080/dir/index.html?id=255&m=hello#top');
    console.log('host=='+myURL.host);
    

      

    作者:十八般武藝
    出处:http://www.cnblogs.com/terry6
    本文版权归属作者和博客园共有,转载请注明出处

  • 相关阅读:
    gflag使用
    INTERVIEW #2
    Decision Tree
    Java FAQ
    K-Nearest Neighbors
    INTERVIEW #1
    C++ FAQ
    INTERVIEW #0
    Selection Sort
    TCP 3-Way Handshake
  • 原文地址:https://www.cnblogs.com/terry6/p/6822528.html
Copyright © 2011-2022 走看看