分享

jQuery插件之-jQuery URL Parser $.url.attr('path')

 CevenCheng 2012-05-24
jQuery插件之-jQuery URL Parser
发表于625 天前 JavaScriptjQuery 暂无评论 ? 被围观 707 views+

jQuery插件Query URL Parser用于解析URLs字符串。通过它我们可以方便地获取协议、主机、端口、查询参数、文件名、路径等等。在一些静态页面需要根据参数来调整一些内容的时候这个插件还是挺有用的。

官方下载(托管在github):http://github.com/allmarkedup/jQuery-URL-Parser

本地下载地址:jQuery-URL-Parser

插件可以返回的数据有下面几项:

1 、来源 – URL本身

2 、协议 – 例如 HTTP,HTTPS,文件等

3 、主机 – 如 blog.,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./information/about/index.html?itemID=2&user=dave)

01// get the protocol
02jQuery.url.attr("protocol"// returns 'http'
03 
04// get the path
05jQuery.url.attr("path"// returns '/information/about/index.html'
06 
07// get the host
08jQuery.url.attr("host"// returns 'blog.'
09 
10// get the value for the itemID query parameter
11jQuery.url.param("itemID"// returns 2
12 
13// get the second segment from the url path
14jQuery.url.segment(2) // returns 'about'

2,使用其他指定的URL

1// set a different URL and return the anchor string
2jQuery.url.setUrl("http://blog./category/javascript/#footer").attr("anchor") // returns 'footer'

今天使用发现了一个小小的 bug ,如果 URL 为 “/product?&typeId=67&ppath=15:31;14:14” , 那么这时 jQuery.url.param('typeId')  则返回 undefind 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多