博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Code: UrlBuilder class in JavaScript
阅读量:7224 次
发布时间:2019-06-29

本文共 3300 字,大约阅读时间需要 11 分钟。

/***********************************************************

 UrlBuilder Class created by JavaScript

 Author: lizhi[at]hit.edu.cn

 Version: 1.0
 Created: 2006.02.21 22:05
 Updated: N/A

 History:

     1. The first version of code created in 2006.02.21 
***********************************************************/
None.giffunction UrlBuilder(url)
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif    this.m_Href = null;
InBlock.gif    this.m_Host = null;
InBlock.gif    this.m_Hostname = null
InBlock.gif    this.m_Port = null;
InBlock.gif    this.m_Protocol = null;
InBlock.gif    this.m_Path = null;
InBlock.gif    this.m_Search = null;
InBlock.gif    this.m_Hash = null;
InBlock.gif    this.m_Params = null
InBlock.gif    this.m_Sucess = false
InBlock.gif    if ( url ) this.Parse(url);
InBlock.gif   
InBlock.gif    this.toString = function()
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif         return '[class UrlBuilder]';
ExpandedSubBlockEnd.gif    };     
ExpandedBlockEnd.gif}
None.gif
None.gifUrlBuilder.prototype.Parse = function(url)
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    var m = url.match(/(\w{3,5}:)\/\/([^\.]+(?:\.[^\.:/]+)+)(?::(\d{1,5}))?\/?/);
InBlock.gif    if ( m )
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif         this.m_Protocol = m[1];
InBlock.gif         this.m_Hostname = m[2]; 
InBlock.gif         this.m_Port = m[3]; 
InBlock.gif         if ( this.m_Port ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{
InBlock.gif             this.m_Host = this.m_Hostname + ':' + this.m_Port;
ExpandedSubBlockEnd.gif         }
InBlock.gif         else
ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{  
InBlock.gif             this.m_Host = m[2];
ExpandedSubBlockEnd.gif         }
InBlock.gif         var indexHash = url.indexOf('#');
InBlock.gif         if ( indexHash != -1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{
InBlock.gif             this.m_Hash = url.substr(indexHash);
ExpandedSubBlockEnd.gif         }
InBlock.gif         else
ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{
InBlock.gif             this.m_Hash = '';
ExpandedSubBlockEnd.gif         }        
InBlock.gif         var indexParams = url.indexOf('?');
InBlock.gif         if ( indexParams != -1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{
InBlock.gif             if ( indexHash != -1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif{
InBlock.gif                  this.m_Search = url.substring(indexParams, indexHash);
ExpandedSubBlockEnd.gif             }
InBlock.gif             else
ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif
InBlock.gif                  this.m_Search = url.substr(indexParams);
ExpandedSubBlockEnd.gif             }
InBlock.gif             this.m_Path = url.substr(indexParams);
ExpandedSubBlockEnd.gif         }
InBlock.gif         else
ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{
InBlock.gif             this.m_Search = '';
ExpandedSubBlockEnd.gif         }
InBlock.gif         this.m_Success = true
InBlock.gif         this.m_Params = null
InBlock.gif         this.m_Href = url;
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif};
None.gif
None.gifUrlBuilder.prototype.GetValue = function(key, encoding)
ExpandedBlockStart.gifContractedBlock.gifdot.gif{
InBlock.gif    if ( !this.m_Params )
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif         if ( this.m_Search )
ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif             this.m_Params = dot.gif{}; 
InBlock.gif             var search = this.m_Search.substring(1);
InBlock.gif             var keyValues = search.split('&');
InBlock.gif             for ( var i=0 ; i < keyValues.length ; ++i )
ExpandedSubBlockStart.gifContractedSubBlock.gif             dot.gif{
InBlock.gif                  var keyValue = keyValues[i];
InBlock.gif                  var index = keyValue.indexOf('=');
InBlock.gif                  if ( index != -1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                  dot.gif{
InBlock.gif                       this.m_Params[keyValue.substring(0, index)] = keyValue.substr(index+1);
ExpandedSubBlockEnd.gif                  }
InBlock.gif                  else
ExpandedSubBlockStart.gifContractedSubBlock.gif                  dot.gif{
InBlock.gif                       this.m_Params[keyValue] = '';
ExpandedSubBlockEnd.gif                  }
ExpandedSubBlockEnd.gif              }  
ExpandedSubBlockEnd.gif         }
ExpandedSubBlockEnd.gif    }
InBlock.gif    encoding = encoding || ''; 
InBlock.gif    switch(encoding.toUpperCase())
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif         case 'UTF8' :
ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{
InBlock.gif              return decodeURI(this.m_Params[key]);
ExpandedSubBlockEnd.gif         }
InBlock.gif         case 'UNICODE' :
ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{
InBlock.gif              return unescape(this.m_Params[key]);
ExpandedSubBlockEnd.gif         }
InBlock.gif         case 'GB2312' : // need VBScript function Chr()
InBlock.gif
         default :
ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{
InBlock.gif              return this.m_Params[key];
ExpandedSubBlockEnd.gif         }
ExpandedSubBlockEnd.gif    }  
ExpandedBlockEnd.gif}

Test Case:

ExpandedBlockStart.gif
ContractedBlock.gif
<
script 
language
="javascript"
>
dot.gif
InBlock.gif
function  TestUrlBuilder()
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif {
InBlock.gif    
var  url  =  
new  UrlBuilder('http:
//
birdshome.cnblogs.com:8080/index.aspx?hl=zh-CN&newwindow=1&q=#abc');
InBlock.gif
    url.GetValue(''); 
InBlock.gif    
var  strParams  =  ''; 
InBlock.gif    
for  ( 
var  key 
in  url.m_Params )
ExpandedSubBlockStart.gif
ContractedSubBlock.gif    
dot.gif {
InBlock.gif         strParams  +=  key  +  '  =  '  +  decodeURI(url.m_Params[key])  +  '\r\n';
ExpandedSubBlockEnd.gif    }  
InBlock.gif    alert('m_Href\t = \t'  +  url.m_Href 
InBlock.gif         +  '\r\nm_Host\t = \t'  +  url.m_Host
InBlock.gif         +  '\r\nm_Hostname\t = \t'  +  url.m_Hostname
InBlock.gif         +  '\r\nm_Port\t = \t'  +  url.m_Port
InBlock.gif         +  '\r\nm_Protocol  = \t'  +  url.m_Protocol
InBlock.gif         +  '\r\nm_Path\t = \t'  +  url.m_Path
InBlock.gif         +  '\r\nm_Search\t = \t'  +  url.m_Search
InBlock.gif         +  '\r\nm_Hash\t = \t'  +  url.m_Hash
InBlock.gif         +  '\r\n\r\n'  +  strParams);    
ExpandedBlockEnd.gif}
None.gif
</
script
>

Result:

None.gifm_Href    =    http://birdshome.cnblogs.com:8080/index.aspx?hl=zh-CN
&newwindow=1
&q=#abc
None.gifm_Host    =    birdshome.cnblogs.com:8080
None.gifm_Hostname =    birdshome.cnblogs.com
None.gifm_Port    =    8080
None.gifm_Protocol =    http:
None.gifm_Path    =    ?hl=zh-CN
&newwindow=1
&q=#abc
None.gifm_Search =    ?hl=zh-CN
&newwindow=1
&q=
None.gifm_Hash    =    #abc
None.gif
None.gifhl = zh-CN
None.gifnewwindow = 1
None.gifq =

Shortage:
    正则表达式:/(\w{3,5}:)\/\/([^\.]+(?:\.[^\.:/]+)+)(?::(\d{1,5}))?\/?/ 不能处理带有用户名和密码的url,同时也不能处理Int32格式的IP地址(如: ),本来支持也容易,但是这两种url的使用频率实在太少了。

本文转自博客园鸟食轩的博客,原文链接:http://www.cnblogs.com/birdshome/,如需转载请自行联系原博主。

你可能感兴趣的文章
CentOS的进程管理二
查看>>
https客户端证书导入
查看>>
用 PreparedStatement 向 SqlServer 中一次性插入多条记录
查看>>
Slackware-2014-0903
查看>>
CentOS下安装JDK1.7
查看>>
LDAP DIT设计参考
查看>>
iptables详解
查看>>
Protostuff 介绍
查看>>
一张图看懂开源许可协议,开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别...
查看>>
参数验证其实可以更简明一点
查看>>
Set up Mule runtime env with mule-standalone-3.6.0
查看>>
Linux基础-linux命令:csplit
查看>>
core_framework —— 基于libev的轻量级lua网络开发框架
查看>>
回到顶部
查看>>
DES/3DES(TripleDES)加密、解密测试数据
查看>>
Maven项目标准目录结构
查看>>
Tomcat 系统架构与设计模式,第 1 部分: 工作原理
查看>>
Hadoop输出参数信息详解(16)
查看>>
ERROR 2002 (HY000): Can't connect to local MySQL错误
查看>>
Java版冒泡排序法
查看>>