This article mainly introduces common javascript Functions and 15 common functions, which are highly practical. If you are interested, please refer to them.
Main content list:
1. Adjust the image size, not out of shape (ff ie compatibility)/cut the image (overflow: hidden)
2. Control the number of texts in the textarea Area
3. Click to display a new window
4. The input box automatically grows with the content
5. Add favorites
6. Set the homepage
7. Determine whether a user exists using Jquery + Ajax
8. Check whether the email format is correct.
9. username (length, English field, etc)
10. News Rolling
11. Only positive integers (used by shopping cart) or positive integers (positive integers and decimals) are allowed)
12. Convert the string to a number.
13. Determine the file format (obtain the file suffix)
14. truncate a string
15. Split the string
Main content:
1. Adjust the image size, not out of shape (ff ie compatibility)
// Usage function DrawImage (ImgD, FitWidth, FitHeight) {var image = new Image (); image. src = ImgD. src; if (image. width> 0 & image. height> 0) {if (image. width/image. height> = FitWidth/FitHeight) {if (image. width> FitWidth) {ImgD. width = FitWidth; ImgD. height = (image. height * FitWidth)/image. width;} else {ImgD. width = image. width; ImgD. height = image. height ;}} else {if (image. height> FitHeight) {ImgD. height = FitHeight; ImgD. width = (image. width * FitHeight)/image. height;} else {ImgD. width = image. width; ImgD. height = image. height ;}}}}
Cut through overflow: hidden:
function clipImage(o, w, h){ var img = new Image(); img.src = o.src; if(img.width >0 && img.height>0) { if(img.width/img.height >= w/h) { if(img.width > w) { o.width = (img.width*h)/img.height; o.height = h; //o.setAttribute("style", "marginLeft:-" + ((o.width-w)/2).toString() + "px"); $(o).css("margin-left", "-"+((o.width-w)/2).toString() + "px"); } else { o.width = img.width; o.height = img.height; } } else { if(img.height > h) { o.height = (img.height*w)/img.width; o.width = w; //o.setAttribute("style", "margin-top:-" + ((o.height-h)/2).toString() + "px"); //$(o).css("style", "margin-top:-" + ((o.height-h)/2).toString() + "px"); $(o).css("margin-top", "-"+((o.height-h)/2).toString() + "px"); } else { o.width = img.width; o.height = img.height; } } } }
Instance:
2. Control the number of texts in the textarea Area
《script》 /** * Calculate how many characters remain in a textarea (jQuery) * @param string textarea * @param int maxLength * @param string p */ function charsRemain(textarea, maxLength, p) { var currentLength = $(textarea).val().length; var charsLeft = maxLength - currentLength; if (charsLeft < 0) { charsLeft = 0; } $("#"+ p +"_charsRemain").html(charsLeft); if (currentLength > maxLength) { var fullText = $(textarea).val().substring(0, maxLength); $(textarea).val(fullText); } } /** * Calculate how many characters remain in a textarea (javascript) * @param string textarea * @param int maxLength * @param string p */ function charsRemain(textarea, maxLength, p) { var currentLength = textarea.value.length; var charsLeft = maxLength - currentLength; if (charsLeft < 0) { charsLeft = 0; } document.getElementById(p +"_charsRemain").innerHTML = charsLeft; if (currentLength > maxLength) { var fullText = textarea.value.substring(0, maxLength); textarea.value = fullText; } } 《script》 250 characters remaining
3. Click to display a new window
// Function g_OpenWindow (pageURL, innerWidth, innerHeight) {var ScreenWidth = screen. availWidth var ScreenHeight = screen. availHeight var StartX = (ScreenWidth-innerWidth)/2 var StartY = (ScreenHeight-innerHeight)/2 var wins = window. open (pageURL, 'openwin', 'left = '+ StartX +', top = '+ StartY +', Width = '+ innerWidth + ', height = '+ innerHeight +', resizable = yes, scrollbars = yes, status = no, toolbar = no, menubar = no, location = no') wins. focus ();}
Java code: