zoukankan      html  css  js  c++  java
  • js中url相关

    首先看一个解析url的函数,网址各部分一般格式为http://domain.com/?search=a#hash

    function parseURL(url) {
        var a =  document.createElement('a');   /造一个a元素
        a.href = url;  //a的链接为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('&'),、//把query里面的问号改为空格,并以数组形式返回,按&分隔。在下例中得到id=225 m=hello
                    len = seg.length, i = 0, s;
                for (i;i<len;i++) {
                    if (!seg[i]) { continue; }//没有seach则忽略
                    s = seg[i].split('=');//否则按等号隔开
                    ret[s[0]] = s[1];  id=225   m=hellow
                }
                return ret;返回上面所求
            })(),
            file: (a.pathname.match(//([^/?#]+)$/i) || [,''])[1],  //pathname为路径名,port后面,search前面,z正则表达式有点乱,头大,以后再分析
            hash: a.hash.replace('#',''),//#top变为top
            path: a.pathname.replace(/^([^/])/,'/$1'),//继续乱
            relative: (a.href.match(/tps?://[^/]+(.+)/) || [,''])[1],
            segments: a.pathname.replace(/^//,'').split('/')//无语,看不下去了
        };
    }

    var myURL = parseURL('http://abc.com:8080/dir/index.html?id=255&m=hello#top');  
     
    myURL.file;     // = 'index.html'
    myURL.hash;     // = 'top'
    myURL.host;     // = 'abc.com'
    myURL.query;    // = '?id=255&m=hello'
    myURL.params;   // = Object = { id: 255, m: hello }
    myURL.path;     // = '/dir/index.html'
    myURL.segments; // = Array = ['dir', 'index.html']
    myURL.port;     // = '8080'
    myURL.protocol; // = 'http'
    myURL.source;   // = 'http://abc.com:8080/dir/index.html?id=255&m=hello#top'
     
  • 相关阅读:
    MySQL Execution Plan--合理利用隐式的业务逻辑
    MySQL Table--MySQL外键
    MySQL倒序索引测试2
    MySQL倒序索引测试1
    CCNA-3.硬件介质
    CCNA-2.OSI 7层网络模型
    CCNA-1.认识网络
    windows本地安装Oracle数据库
    扫码登录实现原理
    phpunit 单元测试
  • 原文地址:https://www.cnblogs.com/cumting/p/6831532.html
Copyright © 2011-2022 走看看