Some good javascript function script code

Source: Internet
Author: User

Js functions that calculate the character Length
Copy codeThe Code is as follows:
Function LEN (str ){
Var I, sum = 0;
For (I = 0; I <str. length; I ++ ){
If (str. charCodeAt (I) >=0) & (str. charCodeAt (I) <= 255 ))
Sum = sum + 1;
Else
Sum = sum + 2;
}
Return sum;
}

Js functions that remove spaces before and after characters
Copy codeThe Code is as follows:
Function TRIM (value) {return value. replace (/^ \ s */, ''). replace (/\ s * $ /,'');}

Image Adaptive size function code
Copy codeThe Code is as follows:
Function imgAutoFit (imgObj, maxWidth, maxHeight ){
Var heightWidth;
Var widthHeight;
HeightWidth = imgObj. offsetHeight/imgObj. offsetWidth;
WidthHeight = imgObj. offsetWidth/imgObj. offsetHeight;
If (imgObj. offsetWidth> maxWidth ){
ImgObj. width = maxWidth;
ImgObj. height = maxWidth * heightWidth;
}
If (imgObj. offsetHeight> maxHeight ){
ImgObj. height = maxHeight;
ImgObj. width = maxHeight * widthHeight;
}
}

Function checkTagIsHave (s, t ){
Var re = new RegExp ("<\\/? "+ T + "(? :(? : \ S | \\/)(? : \ N |.) *?)?> "," Ig ");
Var r = re. test (s );
Re = null;
Return r;
}

Function imgUploadFit (){
Var u = document. getElementsByName ("imgUploadName ");
If (0 = u. length) return;
For (var I = 0; I <u. length; I ++ ){
// ImgAutoFit (u [I], 560,560 );
ImgAutoFit (u [I], 320,320 );
U [I]. onclick = function () {window. open (this. src );}
U [I]. style. cursor = "pointer ";
U [I]. title = "click to view the source image ";
}
}

Function inputContent (theQ, oldContent, re, p ){
Var o, oA = ["msgframeamend", "pingJiaFrame", "touSuFrame"];
For (o in oA ){
If ($ (oA [o]) {
TheQ. editIframe (oA [o]);
If ("msgframeamend" = oA [o]) settimeout(function()[((oaoaoa.contentdoc Doc ument. body. innerHTML = oldContent. replace (re, "") + p [1];}, 100 );
Return;
}
}
}

Function checkCommon (n, v, t ){
Var s = t? "Supplemental question": "Reply content ";
Var o = document. getElementById (n). contentWindow;
If ("msgframereply" = n ){
If (v = ""){
Alert ("enter" + s + "! ");
O. focus ();
Return false;
}
}
If ("" = v. replace (/<\/*? (? : Img | p | pre | br )(? :(? : \ S | \/)(? : \ N |.) *?)?> | \ S |/ig, "") & checkTagIsHave (v, "img ")){
Alert ("For images, please attach some descriptive text! ");
O. focus ();
Return false;
}
If (10000 <LEN (v )){
Alert (s + "exceeds the limit of 5000 words! ");
O. focus ();
Return false;
}
Return true;
}

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.