Web 端 URL 的处理
来源:互联网 发布:网易smtp服务器 端口 编辑:程序博客网 时间:2024/04/19 09:13
Web 端 URL 的处理
URL 的处理
很多时候我们需要从一个URL中提取域名,查询关键字,变量参数值等的数据, 有两种方式,一种是利用window.location 上提供的方式;另一种,是利用 a 链接 dom 节点提供的方法:
先来认识一些 URL :
URL :统一资源定位符( Uniform Resource Location )
完整的URL 由这几个部分构成:scheme://host:port/path?query#fragment
scheme = 通信协议 (常见的 http ,ftp 等);
host = 主机(域名或IP);
port = 端口号;
path = 路径
query = 查询字符串
fragment = 信息片段 (锚点)
### 如何获得这些呢? 举一个url 例子:
// http://www.jb51.net:80/seo/?ver=1.0&id=6#imhere
1、 window.location.href 获得整个URL 字符串 (浏览器上的地址栏)
2、window.location.protocol 协议部分 返回值为 http:
3、window.location.host url 的主机部分 www.jb51.net
4、window.location.port url 的端口部分 80 (默认返回空字符)
5、window.location.pathname url 的路径部分(文件地址) /seo/
6、window.location.search 查询字符串 本例返回: ?ver=1.0&id=6
7、window.location.hash 锚点 #imhere
上面就是对一个路径的基本解析。
### 利用这一原理,稍微扩展一下,就得到了一个更加健壮的解析URL各部分的通用方法了
写入如下:
function parseURL( url ) { // 创建a 标签,利用a 标签的一些属性,完成路由信息提取 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 obj = {}, seg = a.search.replace(/^\?/,'').split('&'), len = seg.length,i=0,s; for( ; i<len; i++){ if(!seg[i]) { continue ; } s = seg[i].split('='); obj[s[0]] = s[1]; } return obj; })(), 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‘);
// 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’
- Web 端 URL 的处理
- web.py处理url
- web.py– URL 处理
- web服务器对url中的相对路径的处理("./", "//","../")
- Web URL中对特殊字符的处理
- python的web 开发基础之url处理
- web开发中,对url键值对的处理。
- Javascript的URL处理
- PHP的URL处理
- URL特殊符号的处理
- PHP的URL处理
- servlet的url处理
- URL的编码处理
- 中文的URL处理
- Web特殊字符处理(SQL URL HTML)
- Web特殊字符处理(SQL URL)
- iOS/Android/Web Url Encode空格处理
- java 及 web中项目url处理
- Oracle导入大数据
- 137. Single Number II
- Leetcode-70. Climbing Stairs
- activity去掉标题和状态栏
- Codeforces 653A: Bear and Three Balls(技巧,水)
- Web 端 URL 的处理
- C++设计模式——单例模式
- 事务概述
- JavaScript强化教程-列表的动态增加元素
- PL/SQL-存储过程、函数
- Xms Xmx PermSize MaxPermSize的含义
- web.xml加载顺序
- jq,js的ajax中调用$(this)
- [LeedCode]242. Valid Anagram