dedecms织梦内容管理系统    
首页 | java | C/C++ | PHP | 操作系统 | ajax | 脚本编程 | 安全技术 | 本站下载页 | 专题 | QQ群 | 测试中心 | 会员中心 | 积分规则
  当前位置:主页>ajax>文章内容
一个非常健全的 Javascript 链接(URL)解析类
来源: 作者:
用 Javascript 解析链接(URL)是一个常见的需求,本文介绍了一个非常健全的用 Javascript 写的链接(URL)解析类,他可以 准确获取一个完整的 URL 中每个部分的内容,包括协议、URL中包含的用户名和密码、主机名、端口、路径名、参数、锚点 (Fragment Anchor)等信息。

其实他的方法就是常见的正则匹配,但是整个实现写的很精巧,非常值得 javascript 新手研究。

Javascript:
  1.  
  2. <script type="text/javascript">
  3.  
  4. // 说明:一个非常健全的 Javascript 链接(URL)解析类
  5. // 整理:http://www.CodeBit.cn
  6.  
  7. /**
  8. * @projectDescription Poly9's polyvalent URLParser class
  9. *
  10. * @author Denis Laprise - denis@poly9.com - http://poly9.com
  11. * @version 0.1
  12. * @namespace Poly9
  13. *
  14. * See the unit test file for more examples.
  15. * URLParser is freely distributable under the terms of an MIT-style license.
  16. */
  17.  
  18. if (typeof Poly9 == 'undefined')
  19. {
  20. var Poly9 = {};
  21. }
  22.  
  23. /**
  24. * Creates an URLParser instance
  25. *
  26. * @classDescription Creates an URLParser instance
  27. * @return {Object} return an URLParser object
  28. * @param {String} url The url to parse
  29. * @constructor
  30. * @exception {String} Throws an exception if the specified url is invalid
  31. */
  32. Poly9.URLParser = function(url) {
  33.  
  34. this._fields = {
  35. 'Username' : 4,
  36. 'Password' : 5,
  37. 'Port' : 7,
  38. 'Protocol' : 2,
  39. 'Host' : 6,
  40. 'Pathname' : 8,
  41. 'URL' : 0,
  42. 'Querystring' : 9,
  43. 'Fragment' : 10
  44. };
  45.  
  46. this._values = {};
  47. this._regex = null;
  48. this.version = 0.1;
  49. this._regex = /^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/;
  50.  
  51. for(var f in this._fields)
  52. {
  53. this['get' + f] = this._makeGetter(f);
  54. }
  55.  
  56. if (typeof url != 'undefined')
  57. {
  58. this._parse(url);
  59. }
  60. }
  61. /**
  62. * @method
  63. * @param {String} url The url to parse
  64. * @exception {String} Throws an exception if the specified url is invalid
  65. */
  66. Poly9.URLParser.prototype.setURL = function(url) {
  67. this._parse(url);
  68. }
  69.  
  70. Poly9.URLParser.prototype._initValues = function() {
  71. for(var f in this._fields)
  72. {
  73. this._values[f] = '';
  74. }
  75. }
  76.  
  77. Poly9.URLParser.prototype._parse = function(url) {
  78. this._initValues();
  79. var r = this._regex.exec(url);
  80. if (!r) throw "DPURLParser::_parse -> Invalid URL";
  81.  
  82. for(var f in this._fields) if (typeof r[this._fields[f]] != 'undefined')
  83. {
  84. this._values[f] = r[this._fields[f]];
  85. }
  86. }
  87.  
  88. Poly9.URLParser.prototype._makeGetter = function(field) {
  89. return function() {
  90. return this._values[field];
  91. }
  92. }
  93.  
  94. </script>
  95.  



示例代码:
Javascript:
  1.  
  2. <script type="text/javascript">
  3.  
  4. var url = 'http://user:password@www.codebit.cn:9901/pub/article.php?offset=10&perpage=10#fragment';
  5.  
  6. var p = new Poly9.URLParser(url);
  7.  
  8. document.write("<strong>URL:</strong> " + url + "<br /><br />");
  9. document.write("解析结果如下:<br /><br />");
  10. document.write("<strong>Protocol:</strong> " + p.getProtocol() + "<br />");
  11. document.write("<strong>Username:</strong> " + p.getUsername() + "<br />");
  12. document.write("<strong>Password:</strong> " + p.getPassword() + "<br />");
  13. document.write("<strong>Host:</strong> " + p.getHost() + "<br />");
  14. document.write("<strong>Port:</strong> " + p.getPort() + "<br />");
  15. document.write("<strong>Pathname:</strong> " + p.getPathname() + "<br />");
  16. document.write("<strong>Query String:</strong> " + p.getQuerystring() + "<br />");
  17. document.write("<strong>Fragment:</strong> " + p.getFragment() + "<br />");
  18.  
  19. </script>
  20.  

上一篇: 在 XHTML-Strict 模式下实现新窗口打开外部链接   下一篇: 用 Javascript 和 CSS 实现脚注(Footnote)效果
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·正则表达式30分钟入门教程
·AJAX技术在PHP开发中的简单应用
·AJAX上手篇
·用AJAX+J2EE实现网上会议室系统
·AJax联手SOA新一代Web2.0应用程
·prototype里的$方法
·警告!Ajax技术的安全问题不容忽
·JavaScript中如何定义类
·JS的流程设计器,我们需要什么
· 通过 Javascript 实现的 CSS 浏
·用 javascript获取页面上有选择
· 滑动式折叠菜单 - Slashdot's
  相关文章
· 在 XHTML-Strict 模式下实现新
· 用 Javascript 和 CSS 实现脚注
· 滑动式折叠菜单 - Slashdot's
·用 javascript获取页面上有选择
· 通过 Javascript 实现的 CSS 浏
·JS的流程设计器,我们需要什么
·JavaScript中如何定义类
·警告!Ajax技术的安全问题不容忽
·prototype里的$方法
·AJax联手SOA新一代Web2.0应用程
·用AJAX+J2EE实现网上会议室系统
·AJAX上手篇
  相关信息
copy right @ 百家拳软件项目研究室 2007 辽ICP备07011763