JavaScript some good function scripting code _javascript Tips

Source: Internet
Author: User

js function for calculating character length

Copy Code code 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;
}

The JS function of the space before and after the character
Copy Code code as follows:

function TRIM (value) {return Value.replace (/^\s*/, '). Replace (/\s*$/, ');}

Picture Adaptive size function code
Copy Code code 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 see the original 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 () {$ (Oa[o)). ContentWindow.document.body.innerHTML = Oldcontent.replace (Re, "") +p[1];},100);
Return
}
}
}

function Checkcommon (n,v,t) {
var s = t? " Supplementary questions ":" Reply Content ";
var O=document.getelementbyid (n). Contentwindow;
if ("msgframereply" = = N) {
if (v = = "") {
Alert ("Please fill in" +s+ "!");
O.focus ();
return false;
}
}
if ("" "==v.replace (/<\/*) (?: IMG|P|PRE|BR) (?:(?: \ s|\/) (?: \ n|.) *?)? >|\s| /ig, "") &&checktagishave (V, "img")) {
Alert ("For pictures, please attach some descriptive text!") ");
O.focus ();
return false;
}
if (10000<len (v)) {
Alert (s+ "More than 5000 words limit!");
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.