前端URL解析函数
来源:互联网 发布:国有企业分配制度 知乎 编辑:程序博客网 时间:2024/05/24 01:00
客户端解析URL的工具函数(来自网络),用起来很不错:
function parseURL(url) { var a = document.createElement('a'); a.href = 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('&'), len = seg.length, i = 0, s; for (;i<len;i++) { if (!seg[i]) { continue; } s = seg[i].split('='); ret[s[0]] = s[1]; } return ret; })(), file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1], hash: a.hash.replace('#',''), 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'); var myURL = parseURL('http://localhost:8080/test/mytest/toLogina.ction?m=123&pid=abc'); //alert(myURL.path); //alert(myURL.params.m); alert(myURL.params.pid);获取参数的使用方法:
var params = parseURL([你的URL]);
获取a参数的值:
var avalue = params.a;
参考:
一个非常全面的javascript URL解析函数和分段URL解析方法
一、URL解析函数
<script>
/**
*@param {string} url 完整的URL地址
*@returns {object} 自定义的对象
*@description 用法示例: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'
*/
function parseURL(url) {
var a = document.createElement('a');
a.href = 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('&'),
len = seg.length, i = 0, s;
for (;i<len;i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
hash: a.hash.replace('#',''),
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');
var myURL = parseURL('http://localhost:8080/test/mytest/toLogina.ction?m=123&pid=abc');
alert(myURL.path);
alert(myURL.params.m);
alert(myURL.params.pid);
</script>
二、JS分段URL解析
URL : 统一资源定位符 (Uniform Resource Locator, URL)
完整的URL由这几个部分构成:scheme://host:port/path?query#fragment
scheme = 通信协议 (常用的http,ftp,maito等)
host = 主机 (域名或IP)
port = 端口号
path = 路径
query = 查询(可选,用于给动态网页(如使用CGI、ISAPI、PHP/JSP/ASP/ASP.NET等技术制作的网页)传递参数,可有多个参数,用”&”符号隔开,每个参数的名和值用”=”符号隔开。)
fragment = 信息片断(字符串,用于指定网络资源中的片断。例如一个网页中有多个名词解释,可使用fragment直接定位到某一名词解释。(也称为锚点.))
对于这样一个URL
http://www.jb51.net:80/seo/?ver=1.0&id=6#imhere
我们可以用javascript获得其中的各个部分
1, window.location.href
整个URl字符串(在浏览器中就是完整的地址栏)
2,window.location.protocol
URL 的协议部分
本例返回值:http:
3,window.location.host
URL 的主机部分
本例返回值:www.jb51.net
4,window.location.port
URL 的端口部分
如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符
本例返回值:”"
5,window.location.pathname
URL 的路径部分(就是文件地址)
本例返回值:/seo/
6,window.location.search
查询(参数)部分
除了给动态语言赋值以外,我们同样可以给静态页面,并使用javascript来获得相信应的参数值
本例返回值:?ver=1.0&id=6
7,window.location.hash
锚点
本例返回值:#imhere
- 前端URL解析函数
- url解析函数
- 介绍一个前端javascript解析url的库
- 后端URL中文传参+前端解析和页面显示
- 一个解析RTSP 的URL函数
- 3个解析url的php函数
- PHP解析URL函数parse_url和parse_str
- js解析URL为对象函数实现
- ParseURL, UrlLike, URL解析函数与匹配函数
- 解析url
- URL解析
- URL解析
- 解析URL
- url解析
- url解析
- url解析
- 解析url
- 解析URL
- 用java编写一个简单的字符串加密解密程序,将字符串分成若干行,实现字符串以一列一列读取,并还原之前输入的字符串。这里实现4行输出。
- Android如何调试更新系统自带的app
- Android 之SegmentControl 分段控件
- Apache配置反向代理、负载均衡和集群(mod_proxy方式)
- C#项目obj和bin文件什么区别和联系
- 前端URL解析函数
- JAVA面试中问及HIBERNATE与 MYBATIS的对比
- Android 6.0监听短信(有Git真好,简单方便+轻松愉快)
- JavaEclipse注释模板
- 欢迎使用CSDN-markdown编辑器
- 如何快速更新Android studio的sdk
- 【JavaScript】匿名函数和闭包
- 安卓7.0已知新特性汇总:这样的Android N你可满意?
- Linux Signal