分类: 系统运维
2010-05-27 21:34:20
/********
* new a random string
* param : length, the returned string length, default is 32
*/
function RandomStr( length )
{
length = length || 32;
var source = "abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "0123456789" + "-@#$%^*()+!~_=";
var random = "";
for(var i = 0;i < length; i++)
random += source.charAt(Math.ceil(Math.
return random;
}
/********
* dump a form to post transmission data
* param : form_id, target id you want to do
* return: { data and boundary }
*/
function DumpFormToPost( form_id )
{
var form = document.getElementById( form_id );
if ( form == null )
{
alert ( "Error : Can not get form by id '" + form_id + "'!");
return null;
}
var boundary = '-------------------------' + RandomStr(12);
var postItem = function( name, value )
{
return "--" + boundary + "\r\n"
+ "Content-Disposition: form-data; "
+ "name=\"" + name + "\"\r\n" + "\r\n" + value + "\r\n";
};
var postdata = '';
var inputs = form.getElementsByTagName('
for( var i=0; i < inputs.length; i++)
{
var input = inputs[i];
if ( input.type == 'text'
|| input.type == 'hidden'
|| input.type == 'password'
|| ( input.type == 'checkbox' && input.checked )
|| ( input.type == 'radio' && input.checked )
) {
postdata += postItem(input.name, input.value );
}
}
var selects = form.getElementsByTagName('
for( var i=0; i < selects.length; i++)
{
var select = selects[i];
for( var j = 0; j < select.options.length; j++ )
{
if ( select.options[j].selected )
{
postdata += postItem( select.name, select.options[j].value );
}
}
}
var texts = form.getElementsByTagName('
for( var i=0; i < texts.length; i++)
{
var text = texts[i];
postdata += postItem( text.name, text.value );
}
if ( postdata != '' )
postdata += "\r\n--" + boundary + "--\r\n";
//alert( postdata );
return { data:postdata, boundary:boundary } ;
}
/********
* new a xmlDocument object from a xml string
* param : xml string to parse
*/
function GetXmlDocObject( xmlStr )
{
var xmlDoc = null;
if (window.ActiveXObject)
{
//for IE
xmlDoc = new ActiveXObject("Microsoft.
xmlDoc.async = false;
xmlDoc.loadXML( xmlStr );
}
else {
var parser=new DOMParser();
xmlDoc = parser.parseFromString( xmlStr, "text/xml" );
}
return xmlDoc;
}
/********
* new a XMLHTTP object
*/
function GetXmlHttpObject()
{
var xmlHttp = null;
if ( window.ActiveXObject )
{
//for IE
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"
}
catch (e) {
xmlHttp=new ActiveXObject("Microsoft.
}
}
else {
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
return xmlHttp;
}
var xmlHttp;
function AjaxLoad()
{
try {
// abort previous request
xmlHttp.abort();
xmlHttp = null;
} catch (e) {}
xmlHttp = GetXmlHttpObject();
if ( xmlHttp == null )
alert ("Error: XMLHTTPRequest can not be created !");
this.url = '/cgi-bin/ajax.cgi';
}
AjaxLoad.prototype = {
OnReady : function()
{
if ( xmlHttp.readyState != 4 ) { return }
if ( xmlHttp.status != 200 )
{
alert ( "Error: Request return status " + xmlHttp.status );
return;
}
alert( xmlHttp.responseText );
},
Send : function( )
{
xmlHttp.onreadystatechange = this.OnReady;
xmlHttp.open( 'GET', this.url, true );
xmlHttp.send( );
}
};
encodeURI( str ):
将字串编码为 URI, 不编码字符包括 , / ? : @ & = + $ #。
encodeURIComponent( str ):
将字串编码为 URIs,因此所有字符都会被编码。
注:不应该用 escape( str ) 进行 URI 的编码,这将是错误的。