直接使用できるJavaScriptユーティリティコードを持ってきてください.
19225 ワード
1、オリジナルJavaScriptは文字列長の切り取りを実現する.
1 function cutstr(str, len) {
2 var temp;
3 var icount = 0;
4 var patrn = /[^\x00-\xff]/;
5 var strre = "";
6 for (var i = 0; i < str.length; i++) {
7 if (icount < len - 1) {
8 temp = str.substr(i, 1);
9 if (patrn.exec(temp) == null) {
10 icount = icount + 1
11 } else {
12 icount = icount + 2
13 }
14 strre += temp
15 } else {
16 break
17 }
18 }
19 return strre + "..."
20 }
2、 JavaScript
1 function getHost(url) {
2 var host = "null";
3 if(typeof url == "undefined"|| null == url) {
4 url = window.location.href;
5 }
6 var regex = /^\w+\:\/\/([^\/]*).*/;
7 var match = url.match(regex);
8 if(typeof match != "undefined" && null != match) {
9 host = match[1];
10 }
11 return host;
12 }
3、 JavaScript
1 String.prototype.trim = function() {
2 var reExtraSpace = /^\s*(.*?)\s+$/;
3 return this.replace(reExtraSpace, "$1")
4 }
4、 JavaScript
1 String.prototype.replaceAll = function(s1, s2) {
2 return this.replace(new RegExp(s1, "gm"), s2)
3 }
5、 JavaScript html
1 function HtmlEncode(text) {
2 return text.replace(/&/g, '&').replace(/\"/g, '"').replace(/</g, '<').replace(/>/g, '>')
3 }
6、 JavaScript html
1 function HtmlDecode(text) {
2 return text.replace(/&/g, '&').replace(/"/g, '\"').replace(/</g, '<').replace(/>/g, '>')
3 }
7、 JavaScript
1 Date.prototype.Format = function(formatStr) {
2 var str = formatStr;
3 var Week = [' ', ' ', ' ', ' ', ' ', ' ', ' '];
4 str = str.replace(/yyyy|YYYY/, this.getFullYear());
5 str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
6 str = str.replace(/MM/, (this.getMonth() + 1) > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
7 str = str.replace(/M/g, (this.getMonth() + 1));
8 str = str.replace(/w|W/g, Week[this.getDay()]);
9 str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
10 str = str.replace(/d|D/g, this.getDate());
11 str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
12 str = str.replace(/h|H/g, this.getHours());
13 str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
14 str = str.replace(/m/g, this.getMinutes());
15 str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
16 str = str.replace(/s|S/g, this.getSeconds());
17 return str
18 }
8、 JavaScript
1 function isDigit(value) {
2 var patrn = /^[0-9]*$/;
3 if (patrn.exec(value) == null || value == "") {
4 return false
5 } else {
6 return true
7 }
8 }
9、 JavaScript cookie
1 function setCookie(name, value, Hours) {
2 var d = new Date();
3 var offset = 8;
4 var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
5 var nd = utc + (3600000 * offset);
6 var exp = new Date(nd);
7 exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000);
8 document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString() + ";domain=360doc.com;"
9 }
10、 JavaScript cookie
1 function getCookie(name) {
2 var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
3 if (arr != null) return unescape(arr[2]);
4 return null
5 }
11、 JavaScript
1 function AddFavorite(sURL, sTitle) {
2 try {
3 window.external.addFavorite(sURL, sTitle)
4 } catch(e) {
5 try {
6 window.sidebar.addPanel(sTitle, sURL, "")
7 } catch(e) {
8 alert(" , Ctrl+D ")
9 }
10 }
11 }
12、 JavaScript IE6
1 var ua = navigator.userAgent.toLowerCase();
2 var isIE6 = ua.indexOf("msie 6") > -1;
3 if (isIE6) {
4 try {
5 document.execCommand("BackgroundImageCache", false, true)
6 } catch(e) {}
7 }
13、 JavaScript
1 function LoadStyle(url) {
2 try {
3 document.createStyleSheet(url)
4 } catch(e) {
5 var cssLink = document.createElement('link');
6 cssLink.rel = 'stylesheet';
7 cssLink.type = 'text/css';
8 cssLink.href = url;
9 var head = document.getElementsByTagName('head')[0];
10 head.appendChild(cssLink)
11 }
12 }
14、 JavaScript
1 function $(id) {
2 return !id ? null : document.getElementById(id);
3 }
4 function display(id) {
5 var obj = $(id);
6 if(obj.style.visibility) {
7 obj.style.visibility = obj.style.visibility == 'visible' ? 'hidden' : 'visible';
8 } else {
9 obj.style.display = obj.style.display == '' ? 'none' : '';
10 }
11 }
15、元のJavaScriptカーソルはテキストの後ろに止まって、テキストボックスがフォーカスを得る時に呼び出します.1 function focusLast(){
2 var e = event.srcElement;
3 var r =e.createTextRange();
4 r.moveStart('character',e.value.length);
5 r.collapse(true);
6 r.select();
7 }
16、 JavaScript URL
1 function getUrlState(URL){
2 var xmlhttp = new ActiveXObject("microsoft.xmlhttp");
3 xmlhttp.Open("GET",URL, false);
4 try{
5 xmlhttp.Send();
6 }catch(e){
7 }finally{
8 var result = xmlhttp.responseText;
9 if(result){
10 if(xmlhttp.Status==200){
11 return(true);
12 }else{
13 return(false);
14 }
15 }else{
16 return(false);
17 }
18 }
19 }
17、 JavaScript checkbox
1 function checkAll() {
2 var selectall = document.getElementById("selectall");
3 var allbox = document.getElementsByName("allbox");
4 if (selectall.checked) {
5 for (var i = 0; i < allbox.length; i++) {
6 allbox[i].checked = true;
7 }
8 } else {
9 for (var i = 0; i < allbox.length; i++) {
10 allbox[i].checked = false;
11 }
12 }
13 }
18、 JavaScript
1 function isMobile(){
2 if (typeof this._isMobile === 'boolean'){
3 return this._isMobile;
4 }
5 var screenWidth = this.getScreenWidth();
6 var fixViewPortsExperiment = rendererModel.runningExperiments.FixViewport || rendererModel.runningExperiments.fixviewport;
7 var fixViewPortsExperimentRunning = fixViewPortsExperiment && (fixViewPortsExperiment.toLowerCase() === "new");
8 if(!fixViewPortsExperiment){
9 if(!this.isAppleMobileDevice()){
10 screenWidth = screenWidth/window.devicePixelRatio;
11 }
12 }
13 var isMobileScreenSize = screenWidth < 600;
14 var isMobileUserAgent = false;
15 this._isMobile = isMobileScreenSize && this.isTouchScreen();
16 return this._isMobile;
17 }
19、 JavaScript
1 function isMobileUserAgent(){
2 return (/iphone|ipod|android.*mobile|windows.*phone|blackberry.*mobile/i.test(window.navigator.userAgent.toLowerCase()));
3 }
20、 JavaScript
1 function isAppleMobileDevice(){
2 return (/iphone|ipod|ipad|Macintosh/i.test(navigator.userAgent.toLowerCase()));
3 }
21、 JavaScript
1 function isAndroidMobileDevice(){
2 return (/android/i.test(navigator.userAgent.toLowerCase()));
3 }
22、 JavaScript
1 String.prototype.startWith = function (s) {
2 return this.indexOf(s) == 0
3 }
23、 JavaScript
1 String.prototype.endWith = function (s) {
2 var d = this.length - s.length;
3 return (d >= 0 && this.lastIndexOf(s) == d)
4 }
24、 JavaScript IE
1 function getIE(){
2 if (window.ActiveXObject){
3 var v = navigator.userAgent.match(/MSIE ([^;]+)/)[1];
4 return parseFloat(v.substring(0, v.indexOf(".")))
5 }
6 return false
7 }
25、 JavaScript
1 function getPageHeight(){
2 var g = document, a = g.body, f = g.documentElement, d = g.compatMode == "BackCompat"
3 ? a
4 : g.documentElement;
5 return Math.max(f.scrollHeight, a.scrollHeight, d.clientHeight);
6 }
26、 JavaScript scrollLeft
1 function getPageScrollLeft(){
2 var a = document;
3 return a.documentElement.scrollLeft || a.body.scrollLeft;
4 }
27、 JavaScript url
1 function removeUrlPrefix(a){
2 a=a.replace(/:/g,":").replace(/./g,".").replace(///g,"/");
3 while(trim(a).toLowerCase().indexOf("http://")==0){
4 a=trim(a.replace(/http:\/\//i,""));
5 }
6 return a;
7 }
28、 JavaScript
1 function uniqueId(){
2 var a=Math.random,b=parseInt;
3 return Number(new Date()).toString()+b(10*a())+b(10*a())+b(10*a());
4 }
29、 JavaScript
1 function checkKey(iKey){
2 if(iKey == 32 || iKey == 229){return true;}/* */
3 if(iKey>47 && iKey < 58){return true;}/* */
4 if(iKey>64 && iKey < 91){return true;}/* */
5 if(iKey>95 && iKey < 108){return true;}/* 1*/
6 if(iKey>108 && iKey < 112){return true;}/* 2*/
7 if(iKey>185 && iKey < 193){return true;}/* 1*/
8 if(iKey>218 && iKey < 223){return true;}/* 2*/
9 return false;
10 }
30、 JavaScript
1 : [\u4e00-\u9fa5]
2 ( ):[^\x00-\xff]
3 :
[\s| ]*\r
4 HTML :<(.*)>.*<\/\1>|<(.*) \/>
5 :(^\s*)|(\s*$)
6 IP :/(\d+)\.(\d+)\.(\d+)\.(\d+)/g
7 Email :\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
8 URL :http://(/[\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
9 sql :^(select|drop|delete|create|update|insert).*$
10 :^\d+$
11 :^[0-9]*[1-9][0-9]*$
12 :^((-\d+)|(0+))$
13 :^-[0-9]*[1-9][0-9]*$
14 :^-?\d+$
15 :^\d+(\.\d+)?$
16 :^((0-9)+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
17 :^((-\d+\.\d+)?)|(0+(\.0+)?))$
18 :^[A-Za-z]+$
19 :^[A-Z]+$
20 :^[a-z]+$
21 :^[A-Za-z0-9]+$
22 :^\w+$
23 E-mail :^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$
24 URL:^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\s*)?$ :^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$
25 :^[1-9]\d{5}$
26 :^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$
27 :^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$
28 ( ):^\x00-\xff
29 :(^\s*)|(\s*$)
30 HTML :<(.*)>.*<\/\1>|<(.*) \/>
31 :
[\s| ]*\r
32 :(h|H)(r|R)(e|E)(f|F) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
33 :\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
34 :(s|S)(r|R)(c|C) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
35 IP :(\d+)\.(\d+)\.(\d+)\.(\d+)
36 :(86)*0*13\d{9}
37 :(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}
38 ( ):(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}
39 :[1-9]{1}(\d+){5}
40 ( ):(-?\d*)\.?\d+
41 :(-?\d*)(\.\d+)?
42 IP:(\d+)\.(\d+)\.(\d+)\.(\d+)
43 :^0\d{2,3}$
44 QQ :^[1-9]*[1-9][0-9]*$
45 ( , 5-16 , ):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
46 、 、 :^[\u4e00-\u9fa5_a-zA-Z0-9]+$
31、 JavaScript
1 function get_radio_value(field){
2 if(field&&field.length){
3 for(var i=0;i<field.length;i++){
4 if(field[i].checked){
5 return field[i].value;
6 }
7 }
8 }else {
9 return ;
10 }
11 }
32、 JavaScript
1 function get_checkbox_value(field){
2 if(field&&field.length){
3 for(var i=0;i<field.length;i++){
4 if(field[i].checked && !field[i].disabled){
5 return field[i].value;
6 }
7 }
8 }else {
9 return;
10 }
11 }
33、 JavaScript
1 function isEmail(str){
2 var re=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
3 if (re.test(str) != true) {
4 return false;
5 }else{
6 return true;
7 }
8 }
34、 JavaScript
1 function isValidReg(chars){
2 var re=/<|>|\[|\]|\{|\}|『|』|※|○|●|◎|§|△|▲|☆|★|◇|◆|□|▼|㊣|﹋|⊕|⊙|〒|ㄅ|ㄆ|ㄇ|ㄈ|ㄉ|ㄊ|ㄋ|ㄌ|ㄍ|ㄎ|ㄏ|ㄐ|ㄑ|ㄒ|ㄓ|ㄔ|ㄕ|ㄖ|ㄗ|ㄘ|ㄙ|ㄚ|ㄛ|ㄜ|ㄝ|ㄞ|ㄟ|ㄢ|ㄣ|ㄤ|ㄥ|ㄦ|ㄧ|ㄨ|ㄩ|■|▄|▆|\*|@|#|\^|\\/;
3 if (re.test( chars) == true) {
4 return false;
5 }else{
6 return true;
7 }
8 }
35、 JavaScript
1 function isValidDecimal( chars ) {
2 var re=/^\d*\.?\d{1,2}$/;
3 if (chars.match(re) == null)
4 return false;
5 else
6 return true;
7 }
36、 JavaScript
1 function isNumber( chars ) {
2 var re=/^\d*$/;
3 if (chars.match(re) == null)
4 return false;
5 else
6 return true;
7 }
37、 JavaScript
1 function isFloat( str ) {
2 for(i=0;i<str.length;i++) {
3 if ((str.charAt(i)<"0" || str.charAt(i)>"9")&& str.charAt(i) != '.'){
4 return false;
5 }
6 }
7 return true;
8 }
38、 JavaScript A-Za-z
1 function isLetters( str ){
2 var re=/^[A-Za-z]+$/;
3 if (str.match(re) == null)
4 return false;
5 else
6 return true;
7 }
39、 JavaScript
1 function isValidPost( chars ) {
2 var re=/^\d{6}$/;
3 if (chars.match(re) == null)
4 return false;
5 else
6 return true;
7 }
40、 JavaScript
1 function IsReverse(text){
2 return text.split('').reverse().join('');
3 }
41、 JavaScript html
1 function clear_script(){
2 K1.value=K1.value.replace(/<script.*?>[\s\S]*?<\/script>|\s+on[a-zA-Z]{3,16}\s?=\s?"[\s\S]*?"|\s+on[a-zA-Z]{3,16}\s?=\s?'[\s\S]*?'|\s+on[a-zA-Z]{3,16}\s?=[^ >]+/ig,"");
3 }