/*********************************************************** 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 ***********************************************************/
function UrlBuilder(url)
![ExpandedBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
this.m_Href =
null;
this.m_Host =
null;
this.m_Hostname =
null;
this.m_Port =
null;
this.m_Protocol =
null;
this.m_Path =
null;
this.m_Search =
null;
this.m_Hash =
null;
this.m_Params =
null;
this.m_Sucess =
false;
if ( url )
this.Parse(url);
this.toString =
function()
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
return '[class UrlBuilder]';
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
};
![ExpandedBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
UrlBuilder.prototype.Parse =
function(url)
![ExpandedBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var m = url.match(/(\w{3,5}:)\/\/([^\.]+(?:\.[^\.:/]+)+)(?::(\d{1,5}))?\/?/);
if ( m )
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
this.m_Protocol = m[1];
this.m_Hostname = m[2];
this.m_Port = m[3];
if (
this.m_Port )
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
this.m_Host =
this.m_Hostname + ':' +
this.m_Port;
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
else![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
this.m_Host = m[2];
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
var indexHash = url.indexOf('#');
if ( indexHash != -1 )
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
this.m_Hash = url.substr(indexHash);
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
else![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
this.m_Hash = '';
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
var indexParams = url.indexOf('?');
if ( indexParams != -1 )
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
if ( indexHash != -1 )
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
this.m_Search = url.substring(indexParams, indexHash);
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
else![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
this.m_Search = url.substr(indexParams);
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
this.m_Path = url.substr(indexParams);
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
else![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
this.m_Search = '';
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
this.m_Success =
true;
this.m_Params =
null;
this.m_Href = url;
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![ExpandedBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
};
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
UrlBuilder.prototype.GetValue =
function(key, encoding)
![ExpandedBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
if ( !
this.m_Params )
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
if (
this.m_Search )
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
this.m_Params =
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{};
var search =
this.m_Search.substring(1);
var keyValues = search.split('&');
for (
var i=0 ; i < keyValues.length ; ++i )
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
var keyValue = keyValues[i];
var index = keyValue.indexOf('=');
if ( index != -1 )
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
this.m_Params[keyValue.substring(0, index)] = keyValue.substr(index+1);
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
else![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
this.m_Params[keyValue] = '';
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![InBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
encoding = encoding || '';
switch(encoding.toUpperCase())
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
case 'UTF8' :
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
return decodeURI(
this.m_Params[key]);
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
case 'UNICODE' :
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
return unescape(
this.m_Params[key]);
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
case 'GB2312' :
// need VBScript function Chr()
default :
![ExpandedSubBlockStart.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
return this.m_Params[key];
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![ExpandedBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
Test Case:
< script language ="javascript" >
function TestUrlBuilder()
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
var url =
new UrlBuilder('http:
// birdshome.cnblogs.com:8080/index.aspx?hl=zh-CN&newwindow=1&q=#abc'); ![InBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
url.GetValue('');
var strParams = '';
for (
var key
in url.m_Params )
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
{
![InBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strParams += key + ' = ' + decodeURI(url.m_Params[key]) + '\r\n';
![ExpandedSubBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![InBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
alert('m_Href\t = \t' + url.m_Href
![InBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
+ '\r\nm_Host\t = \t' + url.m_Host
![InBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
+ '\r\nm_Hostname\t = \t' + url.m_Hostname
![InBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
+ '\r\nm_Port\t = \t' + url.m_Port
![InBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
+ '\r\nm_Protocol = \t' + url.m_Protocol
![InBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
+ '\r\nm_Path\t = \t' + url.m_Path
![InBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
+ '\r\nm_Search\t = \t' + url.m_Search
![InBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
+ '\r\nm_Hash\t = \t' + url.m_Hash
![InBlock.gif](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
+ '\r\n\r\n' + strParams);
![ExpandedBlockEnd.gif](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
</ script > Result:
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
m_Href = http://birdshome.cnblogs.com:8080/index.aspx?hl=zh-CN
&newwindow=1
&q=#abc
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
m_Host = birdshome.cnblogs.com:8080
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
m_Hostname = birdshome.cnblogs.com
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
m_Port = 8080
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
m_Protocol = http:
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
m_Path = ?hl=zh-CN
&newwindow=1
&q=#abc
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
m_Search = ?hl=zh-CN
&newwindow=1
&q=
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
m_Hash = #abc
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
hl = zh-CN
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
newwindow = 1
![None.gif](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
q =
Shortage:
正则表达式:/(\w{3,5}:)\/\/([^\.]+(?:\.[^\.:/]+)+)(?::(\d{1,5}))?\/?/ 不能处理带有用户名和密码的url,同时也不能处理Int32格式的IP地址(如: ),本来支持也容易,但是这两种url的使用频率实在太少了。
本文转自博客园鸟食轩的博客,原文链接:http://www.cnblogs.com/birdshome/,如需转载请自行联系原博主。