(转)jquery.url.js 插件的使用
jQuery插件Query URL Parser用于解析URLs字符串。通过它我们可以方便地获取协议、主机、端口、查询参数、文件名、路径等等。在一些静态页面需要根据参数来调整一些内容的时候这个插件还是挺有用的。
官方下载(托管在github):http://github.com/allmarkedup/jQuery-URL-Parser
本地下载地址:jQuery-URL-Parser
插件可以返回的数据有下面几项:
1 、来源 – URL本身
2 、协议 – 例如 HTTP,HTTPS,文件等
3 、主机 – 如 blog.xiaoningmeng.com,localhost 等
4 、端口 – 例如 80
5 、查询 – 如果它存在的话是整个查询字符串,例如item=value&item2=value2
6 、单个查询字符串参数值
7 、文件 – 该文件名,例如 index.html的
8 、锚 – 哈希(锚)值
9 、路径 – 文件的路径(如/folder/dir/index.html)
10 、相对路径- 包括查询字符串的相对路径(如/folder/dir/index.html?item=value)
11 、目录 – 目录路径(如/folder/dir/)
12 、路径的个别部分
如果需要获取上面的 1、2、3、4、7、8、10、11 项的值可以通过使用 .attr() 方法来获取。
6项可以使用 .param() 方法。
12项可以使用 .segment() 方法。
使用DEMO:
1,使用当前页面的URL(假如地址是http://blog.xiaoningmeng.com/information/about/index.html?itemID=2&user=dave) // get the protocol
jQuery.url.attr("protocol") // returns 'http' // get the path
jQuery.url.attr("path") // returns '/information/about/index.html' // get the host
jQuery.url.attr("host") // returns 'blog.xiaoningmeng.com' // get the value for the itemID query parameter
jQuery.url.param("itemID") // returns 2 // get the second segment from the url path
jQuery.url.segment(2) // returns 'about' 2,使用其他指定的URL
// set a different URL and return the anchor string
jQuery.url.setUrl("http://blog.xiaoningmeng.com/category/javascript/#footer").attr("anchor") // returns 'footer'
// JQuery URL Parser
// Written by Mark Perkins, mark@allmarkedup.com
// License: http://unlicense.org/ (i.e. do what you want with it!) jQuery.url = function()
{
var segments = {};
var parsed = {};
/**
* Options object. Only the URI and strictMode values can be changed via the setters below.
*/
var options = {
url : window.location, // default URI is the page in which the script is running
strictMode: false, // 'loose' parsing by default
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"], // keys available to query
q: {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: {
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, //less intuitive, more accurate to the specs
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // more intuitive, fails on relative paths and deviates from specs
}
};
/**
* Deals with the parsing of the URI according to the regex above.
* Written by Steven Levithan - see credits at top.
*/
var parseUri = function()
{
str = decodeURI( options.url );
var m = options.parser[ options.strictMode ? "strict" : "loose" ].exec( str );
var uri = {};
var i = 14; while ( i-- ) {
uri[ options.key[i] ] = m[i] || "";
} uri[ options.q.name ] = {};
uri[ options.key[12] ].replace( options.q.parser, function ( $0, $1, $2 ) {
if ($1) {
uri[options.q.name][$1] = $2;
}
}); return uri;
}; /**
* Returns the value of the passed in key from the parsed URI.
*
* @param string key The key whose value is required
*/
var key = function( key )
{
if ( jQuery.isEmptyObject(parsed) )
{
setUp(); // if the URI has not been parsed yet then do this first...
}
if ( key == "base" )
{
if ( parsed.port !== null && parsed.port !== "" )
{
return parsed.protocol+"://"+parsed.host+":"+parsed.port+"/";
}
else
{
return parsed.protocol+"://"+parsed.host+"/";
}
}
return ( parsed[key] === "" ) ? null : parsed[key];
};
/**
* Returns the value of the required query string parameter.
*
* @param string item The parameter whose value is required
*/
var param = function( item )
{
if ( jQuery.isEmptyObject(parsed) )
{
setUp(); // if the URI has not been parsed yet then do this first...
}
return ( parsed.queryKey[item] === null ) ? null : parsed.queryKey[item];
}; /**
* 'Constructor' (not really!) function.
* Called whenever the URI changes to kick off re-parsing of the URI and splitting it up into segments.
*/
var setUp = function()
{
parsed = parseUri();
getSegments();
};
/**
* Splits up the body of the URI into segments (i.e. sections delimited by '/')
*/
var getSegments = function()
{
var p = parsed.path;
segments = []; // clear out segments array
segments = parsed.path.length == 1 ? {} : ( p.charAt( p.length - 1 ) == "/" ? p.substring( 1, p.length - 1 ) : path = p.substring( 1 ) ).split("/");
};
return {
/**
* Sets the parsing mode - either strict or loose. Set to loose by default.
*
* @param string mode The mode to set the parser to. Anything apart from a value of 'strict' will set it to loose!
*/
setMode : function( mode )
{
options.strictMode = mode == "strict" ? true : false;
return this;
},
/**
* Sets URI to parse if you don't want to to parse the current page's URI.
* Calling the function with no value for newUri resets it to the current page's URI.
*
* @param string newUri The URI to parse.
*/
setUrl : function( newUri )
{
options.url = newUri === undefined ? window.location : newUri;
setUp();
return this;
},
/**
* Returns the value of the specified URI segment. Segments are numbered from 1 to the number of segments.
* For example the URI http://test.com/about/company/ segment(1) would return 'about'.
*
* If no integer is passed into the function it returns the number of segments in the URI.
*
* @param int pos The position of the segment to return. Can be empty.
*/
segment : function( pos )
{
if ( jQuery.isEmptyObject(parsed) )
{
setUp(); // if the URI has not been parsed yet then do this first...
}
if ( pos === undefined )
{
return segments.length;
}
return ( segments[pos] === "" || segments[pos] === undefined ) ? null : segments[pos];
},
attr : key, // provides public access to private 'key' function - see above
param : param // provides public access to private 'param' function - see above
};
}();
(转)jquery.url.js 插件的使用的更多相关文章
- 异步上传图片,光用jquery不行,得用jquery.form.js插件
异步上传图片,光用jquery不行,得用jquery.form.js插件,百度一下下载这个插件,加jquery,引入就可以了 <form id="postbackground" ...
- jquery.validate.js插件的使用方法
近期做项目.须要用到 jQuery.validate.js插件,于是记录一下工作中的一些经验,以便日后学习. [样例例如以下] 1.前台页面 <form id="form1" ...
- 【PC端】jQuery+PHP实现浏览更多内容(jquery.more.js插件)
参数说明: 'amount' : '10', //每次显示记录数 'address' : 'comments.php', //请求后台的地址 'format' : 'json', //数据传输格式 ' ...
- jQuery.cookie.js插件了解及使用方法
jquery.cookie.js插件实现浏览器的cookie存储,该插件是基于jquery开发,方便cookie使用. jquerycookie.js的下载地址 http://plugins.jque ...
- 购物车增加、减少商品时动画效果:jQuery.Fly.js插件使用方法
某些电商网站加入购物车和减少购物车商品数量时,有个小动画,以抛物线形式增减,如图: 这里用到了第三方jQuery.Fly.js插件(底层依赖Jquery库,地址:https://github ...
- jquery.autocomplete.js 插件的自定义搜索规则
这二天开始用jquery.autocomplete这个自动完成插件.功能基本比较强大,但自己在实际需求中发现还是有一处不足!问题是这样:当我定义了一个本地数据JS文件时,格式为JSON式的数组.如下: ...
- 整屏滚动效果 jquery.fullPage.js插件+CSS3实现
最近很流行整屏滚动的效果,无论是在PC端还是移动端,本人也借机学习了一下,主要通过jquery.funnPage.js插件+CSS3实现效果. 本人做的效果: PC端:http://demo.qpdi ...
- ajax请求执行完成后再执行其他操作(jQuery.page.js插件使用为例)
就我们做知,ajax强大之处在于它的异步请求,但是有时候我们需要ajax执行彻底完成之后再执行其他函数或操作 这个时候往往我们用到ajax的回调函数,但是假如你不想或者不能把接下来的操作写在回调函数中 ...
- jquery.validata.js 插件
一.Validate插件描述 Validate是基于jQuery的一款轻量级验证插件,内置丰富的验证规则,还有灵活的自定义规则接口,HTML.CSS与JS之间的低耦合能让您自由布局和丰富样式,支持in ...
随机推荐
- JSTL与EL之间的千丝万缕
一.关于JSTL和EL: 什么是JSTL? JSTL( JSP Standard Tag Library)是JSP标准 标签库,由apache实现. 什么是EL? EL(Expression Lang ...
- kafka教程
一.理论介绍(一)相关资料1.官方资料,非常详细: http://kafka.apache.org/documentation.html#quickstart2.有一篇翻译版,基本一致,有些细节不 ...
- 使用padding-top实现自适应背景图片
在父级容器中设定最大的宽度,由于背景图片会出现塌陷的情况,有宽度无高度, 则,在图片容器中添加以下属性 padding-top:%(计算方式:图片的高度/图片的宽度*100%) background- ...
- JavaScript不一样的语法
JavaScript除了面向对象的部分,基本语法和C语言类似,但是也有一些自己的特别之处,现总结如下: (1)break和continue后面可以跟label 语法: break labelname; ...
- Nginx并发访问优化
Nginx反向代理并发能力的强弱,直接影响到系统的稳定性.安装Nginx过程,默认配置并不涉及到过多的并发参数,作为产品运行,不得不考虑这些因素.Nginx作为产品运行,官方建议部署到Linux64位 ...
- JAVA程序优化之字符串优化处理
字符串是软件开发中最为重要的对象之一.通常,字符串对象或其等价对象(如char数组),在内存中总是占据了最大的空间块.因此如何高效地处理字符串,必将是提高系统整体性能的关键所在. 1.String对象 ...
- 检测IIS应用程序池对象 回收
function RecycleYourAppPool([string] $poolName){ Import-Module WebAdministration #获取所有Application Po ...
- Powershell调用静态方法
Powershell将信息存储在对象中,每个对象都会有一个具体的类型,简单的文本会以System.String类型存储,日期会以System.DateTime类型存储.任何.NET对象都可以通过Get ...
- App项目升级Xcode7&iOS9(续) - This bundle is invalid. The bundle identifier contains disallowed characters
金田 iOS 9发布已经有2月有余,现在Xcode已经有升级到Xcode7.1,开发环境安装等一系列相关的流程,以及Xcode 7 & iOS 9升级相关的一些部分,在这里就不再多加赘述(详见 ...
- Android 5.0 之SwipeRefreshLayout
金田 下拉刷新是一种比较常用的效果,Android 5.0之前官方并未提供类似的控件,App中主要是用的第三方库,例如PullToRefresh,ActionBar-PullToRefresh等.刚好 ...