Js from the starting point, mainly including String trim, Cookie settings, and other String. prototype. trim = function ()
{
Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");
}
String. prototype. len = function ()
{
Return this. replace (/[^ \ x00-\ xff]/g, 'A'). length;
}
Function StringBuilder (value)
{
This. strings = new Array ("");
This. append (value );
}
// Appends the given value to the end of this instance.
StringBuilder. prototype. append = function (value)
{
If (value)
{
This. strings. push (value );
}
}
// Clears the string buffer
StringBuilder. prototype. clear = function ()
{
This. strings. length = 1;
}
// Converts this instance to a String.
StringBuilder. prototype. toString = function ()
{
Return this. strings. join ("");
}
// String format prototype
// Sample: var test = "my name is {0} {2 }";
// Alert (test. format ('liang ', 'zhonghua ');
If (! String. _ FORMAT_SEPARATOR ){
String. _ FORMAT_SEPARATOR = String. fromCharCode (0x1f );
String. _ FORMAT_ARGS_PATTERN = new RegExp ('^ [^' + String. _ FORMAT_SEPARATOR + '] *'
+ New Array (100). join ('(? :. ([^ '+ String. _ FORMAT_SEPARATOR +'] *)? '));
}
If (! String. format)
{
String. format = function (s ){
Return Array. prototype. join. call (arguments, String. _ FORMAT_SEPARATOR ).
Replace (String. _ FORMAT_ARGS_PATTERN, s );
}
}
If (! ''. Format)
{
String. prototype. format = function (){
Return (String. _ FORMAT_SEPARATOR +
Array. prototype. join. call (arguments, String. _ FORMAT_SEPARATOR )).
Replace (String. _ FORMAT_ARGS_PATTERN, this );
}
}
// End string format
Function checkLoginByCookie ()
{
Var cookieId = "AUTHTEST ";
If (window. location. href. toLowerCase (). indexOf ("qidian.com")>-1)
{
CookieId = "cmfuToken ";
}
If (GetCookie (cookieId )! = Null & GetCookie (cookieId). length> 0 ))
{
Return true;
}
Else
{
If (GetCookie ('cmfu _ al ')! = Null & GetCookie ('cmfu _ al'). length> 0)
{
Return true;
}
}
Return false;
}
Function getUrlParam (name)
{
Var reg = new RegExp ("(^ | &)" + name + "= ([^ &] *) (& | $)", "I ");
Var r = window. location. search. substr (1). match (reg );
If (r! = Null)
{
Return unescape (r [2]);
}
Else
{
Return null;
}
}
Function $ (objName)
{
If (document. getElementById)
{
Return document. getElementById (objName );
}
Else if (document. layers)
{
Return eval ("document. layers ['" + objName + "']");
}
Else
{
Return eval ('document. all. '+ objName );
}
}
Function DateAdd (BaseDate, interval, DatePart)
{
Var dateObj = new Date (BaseDate. replace ("-",","));
Var millisecond = 1;
Var second = Millennium cond * 1000;
Var minute = second * 60;
Var hour = minute * 60;
Var day = hour * 24;
Var year = day * 365;
Var newDate;
Var dVal = new Date (dateObj)
Var dVal = dVal. valueOf ();
Switch (DatePart)
{
Case "ms": newDate = new Date (dVal + millisecond * interval); break;
Case "s": newDate = new Date (dVal + second * interval); break;
Case "mi": newDate = new Date (dVal + minute * interval); break;
Case "h": newDate = new Date (dVal + hour * interval); break;
Case "d": newDate = new Date (dVal + day * interval); break;
Case "y": newDate = new Date (dVal + year * interval); break;
Default: return escape ("the date format is incorrect ");
}
NewDate = new Date (newDate );
Return newDate. getFullYear () + "-" + (newDate. getMonth () + 1) + "-" + newDate. getDate ();
}
// Increase the number of days of the current date
Date. prototype. AddDays = function (interval)
{
Var dateObj = this;
Var millisecond = 1;
Var second = Millennium cond * 1000;
Var minute = second * 60;
Var hour = minute * 60;
Var day = hour * 24;
Var year = day * 365;
Var newDate;
Var dVal = new Date (dateObj)
Var dVal = dVal. valueOf ();
NewDate = new Date (dVal + day * interval );
NewDate = new Date (newDate );
Return newDate
}
Function SetCookie (name, value)
{
Var argv = SetCookie. arguments;
Var argc = SetCookie. arguments. length;
Var expires = (argc> 2 )? Argv [2]. toGMTString (): (new Date (). AddDays (30). toGMTString ();;
Var path = (argc> 3 )? Argv [3]: "/";
Var domain = (argc> 4 )? Argv [4]: null;
Var secure = (argc> 5 )? Argv [5]: false;
Var content = name + "=" + escape (value) + ";";
If (expires! = Null)
{
Content + = "expires =" + expires + ";";
}
If (path! = Null)
{
Content + = "path =" + path + ";";
}
If (domain! = Null)
{
Content + = "domain =" + domain + ";";
}
Document. cookie = content;
}
Function GetCookie (cookieName)
{
Var cookieString = document. cookie;
Var start = cookieString. indexOf (cookieName + '= ');
// The reason for adding the equal sign is to avoid having
// The same string as cookieName.
If (start =-1) // cannot be found
Return null;
Start + = cookieName. length + 1;
Var end = cookieString. indexOf (';', start );
If (end =-1) return unescape (cookieString. substring (start ));
Return unescape (cookieString. substring (start, end ));
}
/* The text box gets the focus */
Function TextBoxOnFocus (txtControl, strDefaultText)
{
If (txtControl. value = strDefaultText)
TxtControl. value = "";
}
/* Text box loses focus */
Function TextBoxOnBlur (txtControl, strDefaultText)
{
If (txtControl. value. replace (/(^ [\ s] *) | ([\ s] * $)/g, "") = "")
TxtControl. value = strDefaultText;
}
/* Function: pop-up message sending window */
Function MultiSendWin (subject, content)
{
Var win = window. open (uploadURL + "? Subject = "+ subject +" & content = "+ content," "," menubar = no, width = 480, height = 550, resizeable = no ","");
Return false;
}
/* Function: the message window is displayed.
Function SpaceSendMsg (toUserId)
{
Var win = window. open (spaceSendMsgURL + "? ToUserId = "+ toUserId," "," menubar = no, width = 500, height = 400, resizeable = no ","");
Return false;
}
*/
Function ShowServerMessage (result)
{
Eval (result. value );
}
// Press enter to submit the form
Function KeydownSubmitForm (btnId)
{
Var btn = document. getElementById (btnId );
If (btn! = Null & event. keyCode = 13)
{
Event. returnValue = false;
Event. keyCode = 9;
Btn. click ();
}
}
// ReadChapter-coupon js
Function MDown (Object ){
Obj = Object. id
Document. all (Obj). setCapture ()
PX = event. x-document.all (Obj). style. pixelLeft;
PY = event. y-document.all (Obj). style. pixelTop;
}
Function MMove (){
If (Obj! = ''){
Document. all (Obj). style. left = event. x-pX;
Document. all (Obj). style. top = event. y-pY;
}
}
Function MUp (){
If (Obj! = ''){
Document. all (Obj). releaseCapture ();
Obj = '';
}
}
// Disable coupon Information
Function LayerClose (pDiscount ){
Document. getElementById (pDiscount). style. visibility = "hidden ";
}
// Display coupon Information
Function LayerShow (pDiscount, discountPrize ){
Var prizeUI = document. getElementById (pDiscount );
PrizeUI. style. left = screen. width-530;
PrizeUI. style. top = screen. Height-480;
PrizeUI. style. visibility = "visible ";
Document. getElementById ("lblPrize1"). innerHTML = discountPrize;
Document. getElementById ("lblPrize2"). innerHTML = discountPrize;
Window. setInterval ("LayerClose ('" + pDiscount + "')", 15000 );
}
// Help masterpage
Function HideMenu (menuid)
{
Var obj = document. getElementById (menuid );
If (obj. style. display = "none ")
{
Obj. style. display = "";
}
Else
{
Obj. style. display = "none ";
}
If (obj. style. display = "")
{
Var tmpId = "M0 ";
For (var I = 1; I <= 9; I ++)
{
Var myid = tmpId + I;
If (myid! = Menuid)
{
Document. getElementById (myid). style. display = "none ";
}
}
}
}
/* P login */
Function ShowLoginDiv ()
{
Var builder = new StringBuilder ();
Builder. append ("
");Builder. append ("
");Builder. append ("
");Builder. append ("
");Builder. append ("
");Builder. append ("
");Builder. append ("
");Builder. append ("
");Builder. append ("
"); Builder. append ("login "); Builder. append (" | "); Builder. append ("× "); Builder. append (" |
"); Builder. append (""); Builder. append (" |
");
// Window. top. scrollTo (0, 0 );
Document. getElementById ("DivMask"). style. height = document. body. scrollHeight;
Document. getElementById ("DivMask"). style. width = document. body. scrollWidth;
Document. getElementById ("DivMask"). style. display = 'block ';
Document. getElementById ("DivLogin"). style. display = "block ";
Document. getElementById ("DivLogin"). innerHTML = builder. toString ();
ScrollDiv ();
Window. onscroll = ScrollDiv;
Window. onresize = ScrollDiv;
Window. onload = ScrollDiv;
}
Function HideLoginMask ()
{
Document. getElementById ("DivMask"). style. display = "none ";
Document. getElementById ("DivLogin"). style. display = "none ";
}
/* Scroll with screen */
Function ScrollDiv ()
{
If ($ ("DivLogin "))
{
Document. getElementById ("DivLogin"). style. top = (document. body. scrollTop +
(Document. body. clientHeight-document.getElementById ("DivLogin"). offsetHeight)/2) + "px ";
Document. getElementById ("DivLogin" ).style.left.pdf (document.doc umentElement. scrollLeft +
(Document. body. clientWidth-document.getElementById ("DivLogin"). offsetWidth)/2) + "px ";
}
If ($ ("AddMark "))
{
// If (! Event)
// Return;
$ ("AddMark"). style. top = document. body. clientHeight + document. body. scrollTop-200
$ ("AddMark"). style. left = document. body. clientWidth-56;
$ ("AddMark"). style. display = '';
// (Document. body. clientHeight-$ ("AddMark"). offsetHeight)/2) + "px ";
If ($ ("MonthVoteTip "))
{
// $ ("MonthVoteTip"). style. top = document. body. scrollTop + document. body. clientHeight-$ ("MonthVoteTip"). offsetHeight + "px ";
$ ("MonthVoteTip"). style. top = getPosition ($ ("AddMark"). y-$ ("AddMark"). offsetHeight + "px ";
}
}
}
Function AutoScroll ()
{
Window. onscroll = ScrollDiv;
Window. onresize = ScrollDiv;
Window. onload = ScrollDiv;
}
Function getPosition (el)
{
For (var lx = 0, ly = 0; el! = Null; lx + = el. offsetLeft, ly + = el. offsetTop, el = el. offsetParent );
Return {x: lx, y: ly}
}
/* 2007-11-28 XuJian */
// Extract a string that contains Chinese characters for processing
// (String, length, increase ...)
Function subString (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;
}
/* 2007-10-26 Get String Lenth (include chinese character )*/
Function GetStringLength (strObj)
{
Return strObj. replace (/[^ \ x00-\ xff]/g, "**"). length;
}
Function addMark (title, url ){
Try {
If (window. sidebar ){
Window. sidebar. addPanel (title, url ,"");
} Else if (document. all ){
Window. external. AddFavorite (url, title );
} Else if (window. opera & window. print ){
Return true;
}
} Catch (e)
{
Alert ("your browser security settings do not allow this operation ")
}
}