JS常用代码片段截取(三)

10.缓存???dialog

window.showModalDialog弹出的 Dialog 无刷新,加上缓存的机制,修改的前端代码不会被即使更新,

在coding的时候比较麻烦,用户使用的时候感本人觉上没什么的大的影响,除非特殊的使用。

为此据说可以在URl上加 &__=new Date(),就能解决



11.focus()& blur()

写代码一定要细心,细节决定成败啊。从下层的意见反馈到上层的回复再到下层的执行,

不晓的要多久才能决定一个feature,由于人人都怕麻烦,依旧决定使用N年前的alert()来进行validate。

粗心的我在使用JQuery的blur()和focus()检查了N旧都不知道错在哪里,当事件失焦的时候触发一个function,

不满足条件是进行alert();forcus()。不晓的咋的,在条件判断中也写了一个forcus(),完了,无线的弹出alert();

死了。在使用此函式的时候一定要逻辑清晰。不过这种也算是一个恶意代码吧。。。



12.JQuery 中$("title").html("XXX")在IE中会出现Unexpected call to method or property access.

主要是因为append()方法。它使用了getDocumentByTagName(),当遇到title的时候使用innerHtml等方法来添加或者

修改title的content时候就会出现这个问题了。

明明javascript中有个document.title的方法。唉,直接使用就好了。

这个还是多亏了NetBeans的对javascript良好的支持,代码补全,尤其是代码提示上的那个解释和Demo。让我为之感动啊

基础不良,记忆超差的我在这里力挺下。Sun被收购,不知道NetBeans下落会如何,伤心ing。。。





13.javascript 使用正则表达式——常用代码验证

// http://docs.jquery.com/Plugins/Validation/Methods/minlength

        minlength: function(value, element, param) {

            return this.optional(element) || this.getLength($.trim(value), element) >= param;

        },

        

        // http://docs.jquery.com/Plugins/Validation/Methods/maxlength

        maxlength: function(value, element, param) {

            return this.optional(element) || this.getLength($.trim(value), element) <= param;

        },

        

        // http://docs.jquery.com/Plugins/Validation/Methods/rangelength

        rangelength: function(value, element, param) {

            var length = this.getLength($.trim(value), element);

            return this.optional(element) || ( length >= param[0] && length <= param[1] );

        },

        

        // http://docs.jquery.com/Plugins/Validation/Methods/min

        min: function( value, element, param ) {

            return this.optional(element) || value >= param;

        },

        

        // http://docs.jquery.com/Plugins/Validation/Methods/max

        max: function( value, element, param ) {

            return this.optional(element) || value <= param;

        },

        

        // http://docs.jquery.com/Plugins/Validation/Methods/range

        range: function( value, element, param ) {

            return this.optional(element) || ( value >= param[0] && value <= param[1] );

        },

        

        // http://docs.jquery.com/Plugins/Validation/Methods/email

        email: function(value, element) {

            // contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/

  
         return this.optional(element) ||
/^((([a-z]|\d|[!#\$%&’*+-\/=\?\^`{|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(.([a-z]|\d|[!#\$%&’*+-\/=\?\^`{|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+))|((\x22)((((\x20|\x09)(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))(((\x20|\x09)(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).?$/i.test(value);

        },

    

        // http://docs.jquery.com/Plugins/Validation/Methods/url

        url: function(value, element) {

            // contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/

  
         return this.optional(element) ||
/^(https?|ftp):\/\/(((([a-z]|\d|-|.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’()\
+,;=]|:)@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]).(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]).(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]).(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).?)(:\d)?)(\/((([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’()*+,;=]|:|@)+(\/(([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’()*+,;=]|:|@)))?)?(\?((([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’()*+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?))?(#((([a-z]|\d|-|.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&’()\+,;=]|:|@)|\/|\?))?$/i.test(value);

        },

        

        // http://docs.jquery.com/Plugins/Validation/Methods/date

        date: function(value, element) {

            return this.optional(element) || !/Invalid|NaN/.test(new Date(value));

        },

    

        // http://docs.jquery.com/Plugins/Validation/Methods/dateISO

        dateISO: function(value, element) {

            return this.optional(element) || /^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(value);

        },

    

        // http://docs.jquery.com/Plugins/Validation/Methods/dateDE

        dateDE: function(value, element) {

            return this.optional(element) || /^\d\d?.\d\d?.\d\d\d?\d?$/.test(value);

        },

    

        // http://docs.jquery.com/Plugins/Validation/Methods/number

        number: function(value, element) {

            return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:.\d+)?$/.test(value);

        },

    

        // http://docs.jquery.com/Plugins/Validation/Methods/numberDE

        numberDE: function(value, element) {

            return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:.\d{3})+)(?:,\d+)?$/.test(value);

        },

        

        // http://docs.jquery.com/Plugins/Validation/Methods/digits

        digits: function(value, element) {

            return this.optional(element) || /^\d+$/.test(value);

        },

        

        // http://docs.jquery.com/Plugins/Validation/Methods/creditcard

        // based on http://en.wikipedia.org/wiki/Luhn

        creditcard: function(value, element) {

            if ( this.optional(element) )

                return "dependency-mismatch";

            // accept only digits and dashes

            if (/[^0-9-]+/.test(value))

                return false;

            var nCheck = 0,

                nDigit = 0,

                bEven = false;



            value = value.replace(/\D/g, "");



            for (n = value.length - 1; n >= 0; n–) {

                var cDigit = value.charAt(n);

                var nDigit = parseInt(cDigit, 10);

                if (bEven) {

                    if ((nDigit
= 2) > 9)

                        nDigit -= 9;

                }

                nCheck += nDigit;

                bEven = !bEven;

            }



            return (nCheck % 10) == 0;

        },

        

        // http://docs.jquery.com/Plugins/Validation/Methods/accept

        accept: function(value, element, param) {

            param = typeof param == "string" ? param.replace(/,/g, ‘|’) : "png|jpe?g|gif";

            return this.optional(element) || value.match(new RegExp(".(" + param + ")$", "i"));

        },

        

        // http://docs.jquery.com/Plugins/Validation/Methods/equalTo

        equalTo: function(value, element, param) {

            return value == $(param).val();

        }