• 收藏一个URL解析的通用方法


      1 var parseUrl = (function () {
      2     function f(g) {
      3         return !isNaN(parseFloat(g)) && isFinite(g)
      4     } return function (t, h) {
      5         var g = h || window.location.toString();
      6         if (!t) {
      7             return g
      8         } else {
      9             t = t.toString()
     10         }
     11 
     12         if (g.substring(0, 2) === "//") {
     13             g = "http:" + g
     14         } else {
     15             if (g.split("://").length === 1) {
     16                 g = "http://" + g
     17             }
     18         }
     19         h = g.split("/");
     20         var l = { auth: "" }, s = h[2].split("@");
     21         if (s.length === 1) {
     22             s = s[0].split(":")
     23         } else {
     24             l.auth = s[0]; s = s[1].split(":")
     25         }
     26 
     27         l.protocol = h[0];
     28         l.hostname = s[0];
     29         l.port = (s[1] || ((l.protocol.split(":")[0].toLowerCase() === "https") ? "443" : "80"));
     30         l.pathname = ((h.length > 3 ? "/" : "") + h.slice(3, h.length).join("/").split("?")[0].split("#")[0]);
     31         var j = l.pathname;
     32         if (j.charAt(j.length - 1) === "/") {
     33             j = j.substring(0, j.length - 1)
     34         }
     35 
     36         var o = l.hostname, p = o.split("."), q = j.split("/");
     37         if (t === "hostname") {
     38             return o
     39         } else {
     40             if (t === "domain") {
     41                 if (/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/.test(o)) {
     42                     return o
     43                 } return p.slice(-2).join(".")
     44             } else {
     45                 if (t === "sub") {
     46                     return p.slice(0, p.length - 2).join(".")
     47                 }
     48                 else {
     49                     if (t === "port") {
     50                         return l.port
     51                     }
     52                     else {
     53                         if (t === "protocol") {
     54                             return l.protocol.split(":")[0]
     55                         }
     56                         else {
     57                             if (t === "auth") {
     58                                 return l.auth
     59                             }
     60                             else {
     61                                 if (t === "user") {
     62                                     return l.auth.split(":")[0]
     63                                 } else {
     64                                     if (t === "pass") {
     65                                         return l.auth.split(":")[1] || ""
     66                                     } else {
     67                                         if (t === "path") {
     68                                             return l.pathname
     69                                         }
     70                                         else {
     71                                             if (t.charAt(0) === ".") {
     72                                                 t = t.substring(1);
     73                                                 if (f(t)) {
     74                                                     t = parseInt(t, 10);
     75                                                     return p[t < 0 ? p.length + t : t - 1] || ""
     76                                                 }
     77                                             }
     78                                             else {
     79                                                 if (f(t)) {
     80                                                     t = parseInt(t, 10);
     81                                                     return q[t < 0 ? q.length + t : t] || ""
     82                                                 }
     83                                                 else {
     84                                                     if (t === "file") {
     85                                                         return q.slice(-1)[0]
     86                                                     } else {
     87                                                         if (t === "filename") {
     88                                                             return q.slice(-1)[0].split(".")[0]
     89                                                         } else {
     90                                                             if (t === "fileext") {
     91                                                                 return q.slice(-1)[0].split(".")[1] || ""
     92                                                             } else {
     93                                                                 if (t.charAt(0) === "?" || t.charAt(0) === "#") {
     94                                                                     var m = g, k = null;
     95                                                                     if (t.charAt(0) === "?") {
     96                                                                         m = (m.split("?")[1] || "").split("#")[0]
     97                                                                     } else {
     98                                                                         if (t.charAt(0) === "#") {
     99                                                                             m = (m.split("#")[1] || "")
    100                                                                         }
    101                                                                     }
    102                                                                     if (!t.charAt(1)) { return m }
    103                                                                     t = t.substring(1);
    104                                                                     m = m.split("&");
    105                                                                     for (var n = 0, r = m.length; n < r; n++) {
    106                                                                         k = m[n].split("=");
    107                                                                         if (k[0] === t) {
    108                                                                             return k[1] || ""
    109                                                                         }
    110                                                                     }
    111                                                                     return null
    112                                                                 }
    113                                                             }
    114                                                         }
    115                                                     }
    116                                                 }
    117                                             }
    118                                         }
    119                                     }
    120                                 }
    121                             }
    122                         }
    123                     }
    124                 }
    125             }
    126         } return ""
    127     }
    128 })();

    调用方式举例:

    parseUrl("?t", "http://a.test.com/bb/cc/test.aspx?username=userA&t=123#aabbcc=1234");//返回userA
    parseUrl("?", "http://a.test.com/bb/cc/test.aspx?username=userA&t=123#aabbcc=1234");//返回username=userA&t=123
    parseUrl("domain", "http://a.test.com/bb/cc/test.aspx?username=userA&t=123#aabbcc=1234");//返回test.com
    parseUrl("sub", "http://a.test.com/bb/cc/test.aspx?username=userA&t=123#aabbcc=1234");//返回a
    // 其它举例可以参考代码
    

      

  • 相关阅读:
    Struts2+Spring+Mybatis+Junit 测试
    struts2 action 页面跳转
    Java面试题(全)
    Java面试题
    Kubernets二进制安装(17)之安装部署Dashboard
    Kubernets二进制安装(16)之安装部署traefik(ingress)
    Kubernets二进制安装(15)之安装部署coredns
    Kubernets二进制安装(14)之flannel之SNAT规则优化
    Kubernets二进制安装(13)之部署Flannel
    Docker网络模型
  • 原文地址:https://www.cnblogs.com/supperniu/p/3467854.html
Copyright © 2020-2023  润新知