Code: UrlBuilder class in JavaScript

 /***********************************************************
 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.gif
dot.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.gif
dot.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.gif
dot.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.gifContractedSubBlock.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.gifContractedSubBlock.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.gif m_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地址(如: http://3396788377/),本来支持也容易,但是这两种url的使用频率实在太少了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值