jquery获取url参数插件

官方网址:
https://github.com/allmarkedup/jQuery-URL-Parser

 

 <script type="text/javascript" src="/Script/jquery.url.js"></script>

 

$.url('http://allmarkedup.com?sky=blue&grass=green').param('sky'); // returns 'blue' 
ExpandedBlockStart.gif View Code
/*
* JQuery URL Parser plugin
* Developed and maintanined by Mark Perkins, mark@allmarkedup.com
* Source repository: https://github.com/allmarkedup/jQuery-URL-Parser
* Licensed under an MIT-style license. See https://github.com/allmarkedup/jQuery-URL-Parser/blob/master/LICENSE for details.
*/

;( function($, undefined) {
    
     var tag2attr = {
        a : 'href',
        img : 'src',
        form : 'action',
        base : 'href',
        script : 'src',
        iframe : 'src',
        link : 'href'
    },
    
key = ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","fragment"],  //  keys available to query

aliases = { "anchor" : "fragment" },  //  aliases for backwards compatability

parser = {
strict : /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,  // less intuitive, more accurate to the specs
loose : /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/  //  more intuitive, fails on relative paths and deviates from specs
},

querystring_parser = /(?:^|&|;)([^&=;]*)=?([^&;]*)/g,  //  supports both ampersand and semicolon-delimted query string key/value pairs

fragment_parser = /(?:^|&|;)([^&=;]*)=?([^&;]*)/g;  //  supports both ampersand and semicolon-delimted fragment key/value pairs

function parseUri( url, strictMode )
{
var str = decodeURI( url ),
res = parser[ strictMode ||  false ? "strict" : "loose" ].exec( str ),
uri = { attr : {}, param : {}, seg : {} },
i = 14;

while ( i-- )
{
uri.attr[ key[i] ] = res[i] || "";
}

//  build query and fragment parameters

uri.param['query'] = {};
uri.param['fragment'] = {};

uri.attr['query'].replace( querystring_parser,  function ( $0, $1, $2 ){
if ($1)
{
uri.param['query'][$1] = $2;
}
});

uri.attr['fragment'].replace( fragment_parser,  function ( $0, $1, $2 ){
if ($1)
{
uri.param['fragment'][$1] = $2;
}
});

//  split path and fragement into segments

        uri.seg['path'] = uri.attr.path.replace(/^\/+|\/+$/g,'').split('/');
        
        uri.seg['fragment'] = uri.attr.fragment.replace(/^\/+|\/+$/g,'').split('/');
        
         //  compile a 'base' domain attribute
        
        uri.attr['base'] = uri.attr.host ? uri.attr.protocol+"://"+uri.attr.host + (uri.attr.port ? ":"+uri.attr.port : '') : '';
        
return uri;
};

function getAttrName( elm )
{
var tn = elm.tagName;
if ( tn !== undefined )  return tag2attr[tn.toLowerCase()];
return tn;
}

$.fn.url =  function( strictMode )
{
var url = '';

if (  this.length )
{
url = $( this).attr( getAttrName( this[0]) ) || '';
}

         return $.url( url, strictMode );
};

$.url =  function( url, strictMode )
{
if ( arguments.length === 1 && url ===  true )
        {
            strictMode =  true;
            url = undefined;
        }
        
        strictMode = strictMode ||  false;
        url = url || window.location.toString();
        
         return {
            
            data : parseUri(url, strictMode),
            
             //  get various attributes from the URI
            attr :  function( attr )
            {
                attr = aliases[attr] || attr;
                 return attr !== undefined ?  this.data.attr[attr] :  this.data.attr;
            },
            
             //  return query string parameters
            param :  function( param )
            {
                 return param !== undefined ?  this.data.param.query[param] :  this.data.param.query;
            },
            
             //  return fragment parameters
            fparam :  function( param )
            {
                 return param !== undefined ?  this.data.param.fragment[param] :  this.data.param.fragment;
            },
            
             //  return path segments
            segment :  function( seg )
            {
                 if ( seg === undefined )
                {
                     return  this.data.seg.path;
                }
                 else
                {
                    seg = seg < 0 ?  this.data.seg.path.length + seg : seg - 1;  //  negative segments count from the end
                     return  this.data.seg.path[seg];
                }
            },
            
             //  return fragment segments
            fsegment :  function( seg )
            {
                 if ( seg === undefined )
                {
                     return  this.data.seg.fragment;
                }
                 else
                {
                    seg = seg < 0 ?  this.data.seg.fragment.length + seg : seg - 1;  //  negative segments count from the end
                     return  this.data.seg.fragment[seg];
                }
            }
            
        };
        
};

})(jQuery);

转载于:https://www.cnblogs.com/VirtualLive/archive/2012/04/23/2465937.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值