web开发小技巧:异类的Javascript处理和解析URL的方式
2014-04-07 23:54
519 查看
今天看到一个关于异类的Javascript处理和解析URL的方式 ,web开发小技巧。转载如下:
<script> 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'); document.write("<br/>"+myURL.file ); // = 'index.html' document.write("<br/>"+myURL.hash ); // = 'top' document.write("<br/>"+myURL.host ); // = 'abc.com' document.write("<br/>"+myURL.query ); // = '?id=255&m=hello' document.write("<br/>"+myURL.params ); // = Object = { id: 255, m: hello } document.write("<br/>"+myURL.path ); // = '/dir/index.html' document.write("<br/>"+myURL.relative); // = '/dir/index.html?id=255&m=hello#top' document.write("<br/>"+myURL.segments); // = Array = ['dir', 'index.html'] document.write("<br/>"+myURL.port ); // = '8080' document.write("<br/>"+myURL.protocol); // = 'http' document.write("<br/>"+myURL.source ); // = 'http://abc.com:8080/dir/index.html?id=255&m=hello#top' document.write("<br/>"); for(var i in myURL.params) document.write("<br/>"+i+":"+myURL.params[i]); </script> http://www.gbtags.com/gb/share/2987.htm
相关文章推荐
- 异类的Javascript处理和解析URL的方式
- 异类的Javascript处理和解析URL的方式
- 异类的Javascript处理和解析URL的方式
- Web开发常用小技巧 -- JavaScript[不断添加中...]
- Web前端开发最佳实践(11):使用更严格的JavaScript编码方式,提高代码质量
- web开发中,对url键值对的处理。
- iOS学习笔记77-ios开发,javascript直接调用oc代码而非通过改变url回调方式
- ios开发,javascript直接调用oc代码而非通过改变url回调方式
- 基于MVC+EasyUI的Web开发框架经验总结(12)--利用Jquery处理数据交互的几种方式
- 【web开发】Javascript点点小技巧
- javascript小技巧,适合做web开发的人
- 转载--关于WEB开发中引入javascript文件方式的一点建议
- ios开发,javascript直接调用oc代码而非通过改变url回调方式
- iOS开发 WKWebView使用第三方库WebViewJavascriptBridge 以及js端处理
- WebView实例开发之人人网Oauth2认证(含URL解析,json解析)
- 要是 IOS 开发的哥们告诉大家说webview 和 本地交互只能通过 “拦截Url” 的方式。。。
- iOS开发中浏览器能打开的URL,WebView打不开的处理方法
- ios开发,javascript直接调用oc代码而非通过改变url回调方式
- ios开发中,关于javascript直接调用oc代码而非通过改变url回调方式
- JavaWeb开发常见乱码处理之设置编码方式