jQuery 获取 URL信息
jQuery获取URL信息有很多方法,但是使用这个插件就非常爽了。
托管地址在:http://github.com/allmarkedup/jQuery-URL-Parser

// http: //localhost:19090/home/index?id=1
var source = $.url.attr("source"); // http://localhost:19090/home/index?id=1
var protocol = $.url.attr("protocol"); // http 全部可选值http, https, file, etc
var path = $.url.attr("path"); // /home/index
var host = $.url.attr("host"); // localhost
var port = $.url.attr("port"); // 19090
var relative = $.url.attr("relative"); // /home/index?id=1
var directory = $.url.attr("directory");// /home/index // http://localhost:19090/floder/index.html?id=1
var file = $.url.attr("file"); // index.html
var query = $.url.attr("query"); // id = 1 这个还可以调用 .param() var id = $.url.param("id"); // 1;
var segment = $.url.segment(1); // 0:floder 1:index.html //也可以指定其他URL
var setUrlAnchor = $.url.setUrl("http://www.baidu.com/#logo").attr("anchor"); // 此时setUrlAnchor与上面示例的 $.url同样调用处理喊出

jQuery.URL.Parse源代码如下:

// 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信息的更多相关文章
- 使用jquery获取url以及jquery获取url参数的方法
使用jquery获取url以及使用jquery获取url参数是我们经常要用到的操作 1.jquery获取url很简单,代码如下 1.window.location.href; 其实只是用到了javas ...
- 一个用php实现的获取URL信息的类
获取URL信息的类 使用这个类,你能获得URL的如下信息: - Host - Path - Statuscode (eg. 404,200, ...) - HTTP Version - Ser ...
- jquery获取url参数
js/jquery 获取url参数 2010年04月27日 星期二 13:45 js代码: function GetQueryString(name) { var reg = new RegExp(& ...
- 使用jquery获取url及url参数的方法及定义JQuery扩展方法
1.jquery获取url很简单,代码如下: window.location.href; 其实只是用到了javascript的基础的window对象,并没有用jquery的知识. 2.jquery获取 ...
- Jquery 获取URL参数
使用jquery获取url以及使用jquery获取url参数是我们经常要用到的操作 1.jquery获取url很简单,代码如下 1.window.location.href; 其实只是用到了javas ...
- [开发笔记]-使用jquery获取url及url参数的方法
使用jquery获取url以及使用jquery获取url参数是我们经常要用到的操作 1.jquery获取url很简单,代码如下: window.location.href; 其实只是用到了javasc ...
- 使用jquery获取url及url参数的方法
使用jquery获取url以及使用jquery获取url参数是我们经常要用到的操作 1.jquery获取url很简单,代码如下: window.location.href; 其实只是用到了javasc ...
- jquery 获取URL参数并转码的例子
通过jquery 获取URL参数并进行转码,个人觉得不错,因为有时不转码就会有乱码的问题.jquery 获取URL参数并转码,首先构造一个含有目标参数的正则表达式对象,匹配目标参数并返回参数值代码: ...
- 使用jquery获取url上的参数(笔记)
使用jquery获取url上的参数(笔记) 一.做作业时经常要获取url上的参数 1.当url上有多个参数时 从互联网找到了一个方法 (function ($) { $.getUrlParam = f ...
随机推荐
- SATA1.0,2.0,3.0区别
外观没区别,接口都一样,线也一样,就是传输速率不一样,控制芯片不一样SATA1.0理论传输速度为1.5Gbit/s SATA2.0理论传输速度为3Gbit/sSATA2.0理论传输速度为6Gbit/s ...
- 提高效率的便签By番茄时间管理 win7标签,小功能,大作用
今日待办 把一些重要的事情,列入其中. 着重处理. 活动清单 罗列一些最近需要做的事情,不一定按照紧急重要的程度. 把活动清单中的事情,按照实际情况,安排到今日待办当中. 还有一个我喜欢的'头脑风暴' ...
- bst 二叉搜索树简单实现
//数组实现二叉树: // 1.下标为零的元素为根节点,没有父节点 // 2.节点i的左儿子是2*i+1:右儿子2*i+2:父节点(i-1)/2: // 3.下标i为奇数则该节点有有兄弟,否则又左兄弟 ...
- Java 安装配置
1.下载 进入官方网站,点击下载链接进入下载页面,选择合适的版本(如,jdk-6u31-windows-i586.exe)下载. 2.安装 双击jdk-6u31-windows-i586.exe文件, ...
- RTP/RTCP/RTSP/RSVP/SDP
RTP Real-time Transport Protocol)是用于Internet上针对多媒体数据流的一种传输层协议.RTP协议详细说明了在互联网上传递音频和视频的标准数据包格式.RTP协议常用 ...
- add BOM to fix UTF-8 in Excel
fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
- URLConnection的连接、超时、关闭用法总结
Java中可以使用HttpURLConnection来请求WEB资源. 1. URL请求的类别 分为二类,GET与POST请求.二者的区别在于: a:) get请求可以获取静态页面,也可以把 ...
- FileUtils.copyDirectory without .SVN
方法1:列出所有文件逐个筛选: File selectedFolder = new File(path); // path to folder to list final IOFileFilter d ...
- (转)25个增强iOS应用程序性能的提示和技巧--中级篇
在性能优化时,当你碰到一些复杂的问题,应该注意和使用如下技巧: 9.重用和延迟加载View10.缓存.缓存.缓存11.考虑绘制12.处理内存警告13.重用花销很大的对象14.使用Sprite Shee ...
- uva 1291(dp)
题意:有一台跳舞机,中间是0.上左下右分别代表1 2 3 4,初始状态人站在中间.两仅仅脚都踏在0上,然后给出一段序列以0为结束,要按顺序踩出来,从0踏到四个方向须要消耗2点能量,从一个方向到相邻的方 ...