将URL解析成像location一样的对象
来源:互联网 发布:织梦tag标签调用代码 编辑:程序博客网 时间:2024/05/17 07:58
之前使用Java的API命名做了解析location.search的例子
现在404页面需要分析document.referrer,不能直接拿到search字符串,所以还得想办法把URL解析成像Location一样的对象
可以想象,这种方法浏览器和服务器必须会有,JavaScript实现应该也有了,不过还是写一个玩玩,以显示个人风格:
function parseLocation(url){if((typeof url)!="string"||url==""){return null;}var urlExp=/^(http:|https:|file:)(?:\/\/)([^\/]*)([^?#]*)([^#]*)(.*)$/ig,hostExp=/^([^:]*)((:\d+)?)$/ig;var protocol="",host="",hostname="",port="",pathname="",search="",hash="";url.replace(urlExp,function(){var args=arguments;protocol=args[1];host=args[2];pathname=args[3];search=args[4];hash=args[5];});host.replace(hostExp,function(){var args=arguments;hostname=args[1];port=args[2].replace(":","");});return protocol?{protocol:protocol,host:host,hostname:hostname,port:port,pathname:pathname,search:search,hash:hash}:null;}//have a test(function(){var url="https://www.google.com.hk/search?q=404&rlz=1C1CHRY_enCN481CN481&sugexp=chrome,mod=16&sourceid=chrome&ie=UTF-8#hash";var loc=parseLocation(url);console.log(loc);})();
非常简洁,Bug肯定会有,毕竟我没去了解标准URL的规范
[2013-01-27]更新解析方式,使用浏览器的解析方式解析
/** * parse a location url to a location like object * @param {Object} window */(function(window){var a=window.document.createElement("a");window.parseLocation=function(url){if(!url){return null;}a.href=url;return {href:a.href,origin:a.origin||a.protocol+"//"+a.host,protocol:a.protocol,host:a.host,hostname:a.hostname,port:a.port,pathname:a.pathname,search:a.search,hash:a.hash};};})(window);
- 将URL解析成像location一样的对象
- location的search操作——解析url字符串,返回包含所有参数的一个对象
- 将url的查询参数解析成字典对象
- 将url的查询参数解析成字典对象
- 将url的查询参数解析成字典对象
- 小孔成像的解析-20151205
- 使用location.search解析URL中?后的请求信息
- 输入一个10000以内的浮点数,然后将该浮点数转化成像读取人民币一样的字符串,打印出来
- window.location.hash解析(URL中的?#)
- 简单解析url参数的js对象。
- location的解析过程
- javascript的location对象
- javascript的location对象
- js的Location对象
- location的hash对象
- JavaScript的location对象
- 解析URL对象
- window对象的location对象
- 计算1+1/3+1/5+...+1/(2n+1)的值
- va_list、va_start、va_arg、va_end的原理与使用
- Invalid location of tag 解决办法分析
- 青涩的思绪 泛起粉色的涟漪
- 《Java数据结构和算法》第二版 Robert lafore 编程作业 第十章
- 将URL解析成像location一样的对象
- Android 鼠标事件 (单击,双击,滑动)
- 餐桌上的“爱心菜”
- SDL入门教程(九):3、文本反馈“按键”信息
- wifi定位监控系统的行业应用
- SDL入门教程(九):4、int转换为std::string
- 关于html中 邮箱链接的问题
- [转]动态库与静态库的原理介绍
- hibernate批量删除