CCPryJS class library code_js object-oriented

Source: Internet
Author: User
The CCPryJS class library code can be referenced by a friend. The Code is as follows:


Function CCPry (){
//
// Determine the browser type
//
This. Browser = {
"IsMozilla" :( typeof document. implementation! = 'Undefined') & (typeof document. implementation. createDocument! = 'Undefined') & (typeof HTMLDocument! = 'Undefined '),
"IsIE": window. ActiveXObject? True: false,
"IsFirefox": navigator. userAgent. toLowerCase (). indexOf ("firefox ")! =-1,
"IsOpera": navigator. userAgent. toLowerCase (). indexOf ("opera ")! =-1
};

//
// Return the DOM object based on the element ID
//
This. $ Id = function (id ){
Return document. getElementById (id );
};
//
// Return the DOM object based on the element ID
//
This. Id = function (id ){
Var DomObjId = this. $ Id (id );
If (! DomObjId) {throw new Error ("No Object! ");}
// Return or set the innerHTML attribute of the object
This. Html = function (html ){
If (! This. IsUndefined (DomObjId. innerHTML ))
{
If (! This. IsNull (html )){
DomObjId. innerHTML = html;
}
Else {return DomObjId. innerHTML };
}
Else {throw new Error ("Object does not support the property innerHTML! ");};
};
// Return or set the innerText attribute of the object
This. Text = function (text ){
If (! This. IsUndefined (DomObjId. innerText ))
{
If (! This. IsNull (text )){
DomObjId. innerText = text;
}
Else {return DomObjId. innerText };
}
Else {throw new Error ("Object does not support the property innerText! ");};
};
// Return or set the value attribute of the object
This. Val = function (val ){
If (! This. IsUndefined (DomObjId. value ))
{
If (! This. IsNull (val )){
DomObjId. value = val;
}
Else {return DomObjId. value };
}
Else {throw new Error ("Object does not support the property value! ");};
}
Return this;
};

//
// Return the DOM object based on the element Name
//
This. $ Name = function (name ){
Return document. getElementsByName (name );
};

//
// Judge whether the string is null or null
//
This. IsNullOrEmpty = function (str ){
If (str = null ){
Return true;
}
Else {
If (str. length <= 0 ){
Return true;
}
}
Return false;
};
//
// Judge whether the string is null
//
This. IsEmpty = function (str ){
If (str. length <= 0 ){
Return true;
}
Return false;
};
//
// Judge whether the string is null
//
This. IsNull = function (str ){
If (str = null ){
Return true;
}
Return false;
};
//
// Determine whether it is a function
//
This. IsFunction = function (fn ){
Return typeof (fn) = "function ";
};

//
// Determine whether it is an object
//
This. IsObject = function (fn ){
Return typeof (fn) = "object ";
};
//
// Determine whether it is a string
//
This. IsString = function (fn ){
Return typeof (fn) = "string ";
};

//
// Determine whether the data type is digital
//
This. IsNumber = function (fn ){
Return typeof (fn) = "number ";
};

//
// Determine whether it is a Boolean Type
//
This. IsBoolean = function (fn ){
Return typeof (fn) = "boolean ";
};

//
// Determine whether or not it is undefined
//
This. IsUndefined = function (fn ){
Return typeof (fn) = "undefined ";
};

//
// Determine whether it is a date type
//
This. IsDate = function (fn ){
Return fn. constructor = Date;
};

//
// Return the Ajax object
//
This. Ajax = function (s ){

// Construct the XMLHttpRequest object
GetXmlHttpRequest = function (){
Var xmlHttpRequest;
If (window. XMLHttpRequest) {xmlHttpRequest = new XMLHttpRequest ();}
Else if (window. ActiveXObject ){
Try {xmlHttpRequest = new ActiveXObject ("Msxml2.XMLHTTP ");}
Catch (e) {xmlHttpRequest = new ActiveXObject ("Microsoft. XMLHTTP ");}
}
If (! XmlHttpRequest ){
Alert ("failed to create XMLHttpRequest object ");
Return null;
}
Return xmlHttpRequest;
};

// Default Ajax Configuration
AjaxSettings = {
Url: "/ajax/ProcessAjax. ashx ",
Method: "POST ",
Timeout: 0,
MimeType: "application/x-www-form-urlencoded ",
Async: true,
Data: null,
Datatype: "html ",
Charset: "UTF-8 ",
Accepts :{
Xml: "application/xml, text/xml ",
Html: "text/html ",
Script: "text/javascript, application/javascript ",
Json: "application/json, text/javascript ",
Text: "text/plain ",
_ Default :"*/*"
},
///


/// Call the function at the request start
///
OnStart: function (){
// Start
},
///
/// Callback function after successful request
///
/// Server return data
OnSuccess: function (msg ){
// Success
},
///
/// Call a function with an operation exception
///
/// Exception Information
OnException: function (msg ){
// Exception
},
///
/// Call the function after the request times out
///
OnTimeout: function (){
// Timeout
},
///
/// Call the function after the request is complete
///
OnComplate: function (){
// Complate
}
};

If (this. IsObject (s )){

/* Check the input object */
AjaxSettings. url = (! This. IsUndefined (s. url) & this. IsString (s. url ))? S. url: ajaxSettings. url;
AjaxSettings. method = (! This. IsUndefined (s. method) & this. IsString (s. method ))? S. method: ajaxSettings. method;
AjaxSettings. timeout = (! This. IsUndefined (s. timeout) & this. IsNumber (s. timeout ))? S. timeout: ajaxSettings. timeout;
AjaxSettings. mimeType = (! This. IsUndefined (s. mimeType) & this. IsString (s. mimeType ))? S. mimeType: ajaxSettings. mimeType;
AjaxSettings. async = (! This. IsUndefined (s. async) & this. IsBoolean (s. async ))? S. async: ajaxSettings. async;
AjaxSettings. data = (! This. IsUndefined (s. data) & this. IsString (s. data ))? S. data: ajaxSettings. data;
AjaxSettings. datatype = (! This. IsUndefined (s. datatype) & this. IsString (s. datatype ))? S. datatype: ajaxSettings. datatype;
AjaxSettings. charset = (! This. IsUndefined (s. charset) & this. IsString (s. charset ))? S. charset: ajaxSettings. charset;
AjaxSettings. OnStart = (! This. IsUndefined (s. OnStart) & this. IsFunction (s. OnStart ))? S. OnStart: ajaxSettings. OnStart;
AjaxSettings. OnSuccess = (! This. IsUndefined (s. OnSuccess) & this. IsFunction (s. OnSuccess ))? S. OnSuccess: ajaxSettings. OnSuccess;
AjaxSettings. OnException = (! This. IsUndefined (s. OnException) & this. IsFunction (s. OnException ))? S. OnException: ajaxSettings. OnException;
AjaxSettings. OnTimeout = (! This. IsUndefined (s. OnTimeout) & this. IsFunction (s. OnTimeout ))? S. OnTimeout: ajaxSettings. OnTimeout;
AjaxSettings. OnComplate = (! This. IsUndefined (s. OnComplate) & this. IsFunction (s. OnComplate ))? S. OnComplate: ajaxSettings. OnComplate;

// Assign a value to xmlhttp and pass in the XMLHttpRequest object
Var xmlhttp = GetXmlHttpRequest ();
Var requestDone = false;
Try
{
// Determine the parameters to be passed in xmlhttp. send () based on the POST or GET Method
If (ajaxSettings. data & ajaxSettings. method. toUpperCase () = "GET "){
AjaxSettings. url + = (ajaxSettings. url. match (/\? /)? "&":"? ") + AjaxSettings. data;
AjaxSettings. data = null;
}
Xmlhttp. open (ajaxSettings. method, ajaxSettings. url, ajaxSettings. async );
Xmlhttp. setRequestHeader ("Content-Type", ajaxSettings. mimeType );
Xmlhttp. setRequestHeader ("X-Requested-With", "XMLHttpRequest"); // you can specify the title of an XMLHttpRequest request.
Xmlhttp. setRequestHeader ("Accept", ajaxSettings. datatype & ajaxSettings. accepts [ajaxSettings. datatype]? AjaxSettings. accepts [ajaxSettings. datatype] + ", */*": ajaxSettings. accepts. _ default );
If (ajaxSettings. timeout> 0 ){
Var timer = setTimeout (function () {requestDone = true; xmlhttp. abort () ;}, ajaxSettings. timeout );
}
Var xmlreadystatechange = function ()
{
If (requestDone) {ajaxSettings. OnTimeout ();} // timeout
Else if (xmlhttp. readyState = 4) // success
{
If (timer) {clearTimeout (timer); timer = null ;}
If (xmlhttp. status = 200 | xmlhttp. status = "success ")
{
Switch (ajaxSettings. datatype. toLowerCase ())
{
Case "html ":
AjaxSettings. OnSuccess (xmlhttp. responseText );
Break;
Case "xml ":
AjaxSettings. OnSuccess (xmlhttp. responseXML );
Break;
}
}
AjaxSettings. OnComplate (); // complate
If (ajaxSettings. async & xmlhttp) {xmlhttp = null ;}
}
Else {ajaxSettings. OnStart ();} // start
}
Xmlhttp. onreadystatechange = xmlreadystatechange;
Xmlhttp. send (ajaxSettings. data );
If (! AjaxSettings. async) {xmlreadystatechange (); xmlhttp = null ;}
}
Catch (e ){
AjaxSettings. OnException (e. message); // exception
}
}
Return this;
}
//
// Split the JSON object and connect it with a specific symbol to form a string
//
This. SplitJson = function (jsonObj, splitSign ){

Var jsonStr = '', signLastIndex;
For (jo in jsonObj)
{
JsonStr + = (jo + "=" + jsonObj [jo] + splitSign );
}
SignLastIndex = jsonStr. lastIndexOf (splitSign );
If (signLastIndex! =-1 ){
JsonStr = jsonStr. substring (0, signLastIndex );
}
Return jsonStr;
}
//
// Scaling down JS Images
//
This. DrawImage = function (ImgD, iwidth, iheight ){
Var flag = false;
Var image = new Image ();
Image. src = ImgD. src;
If (image. width> 0 & image. height> 0)
{
Flag = true;
If (image. width/image. height> = iwidth/iheight)
{
If (image. width> iwidth)
{
ImgD. width = iwidth;
ImgD. height = (image. height * iwidth)/image. width;
}
Else
{
ImgD. width = image. width;
ImgD. height = image. height;
}
}
Else
{
If (image. height> iheight)
{
ImgD. height = iheight;
ImgD. width = (image. width * iheight)/image. height;
}
Else
{
ImgD. width = image. width;
ImgD. height = image. height;
}
}
}
}
//
// Extract a string that contains Chinese characters for processing
//
This. SubString = function (str, len, hasDot ){
Var newLength = 0;
Var newStr = "";
Var chineseRegex =/[^ \ x00-\ xff]/g;
Var singleChar = "";
Var strLength = str. replace (chineseRegex, "**"). length;
For (var I = 0; I <strLength; I ++)
{
SingleChar = str. charAt (I). toString ();
If (singleChar. match (chineseRegex )! = Null)
{
NewLength + = 2;
}
Else
{
NewLength ++;
}
If (newLength> len)
{
Break;
}
NewStr + = singleChar;
}
If (hasDot & strLength> len)
{
NewStr + = "";
}
Return newStr;
}
//
// Replace all
//
This. ReplaceAll = function (Str, oldString, newString ){
Return Str. replace (new RegExp (oldString, "gm"), newString );
},
//
// Obtain the URL parameter. If the URL parameter is invalid, undfined is returned.
//
This. RequestQueryString = function (url ){
Var qIndex = url. indexOf ('? ');
Var queryObj = {};
If (qIndex! =-1 ){
Var queryStr = url. substring (qIndex + 1, url. length );
If (queryStr. indexOf ('&')! =-1 ){
Var arrQuery = new Array ();
ArrQuery = queryStr. split ('&');
For (arrStr in arrQuery ){
ParamKey = arrQuery [arrStr]. substring (0, arrQuery [arrStr]. indexOf ("="). toLowerCase ();
ParamValue = arrQuery [arrStr]. substring (arrQuery [arrStr]. indexOf ("=") + 1, arrQuery [arrStr]. length );
QueryObj [paramKey] = paramValue
}
}
Else {
ParamKey = queryStr. substring (0, queryStr. indexOf ("="). toLowerCase ();
ParamValue = queryStr. substring (queryStr. indexOf ("=") + 1, queryStr. length );
QueryObj [paramKey] = paramValue;
}
}
Return queryObj;
}
}
Window. $ CC = new CCPry ();
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.