I'm not familiar with the JavaScript syntax of "+new" in the now() function in jQuery 1.2.5. Would you explain the plus (+) operator? How does it work? What does it mean? function now(){ return +new Date; } Is it the same as the following? function now(){ return new Date(); }
This method makes the 'id' and 'name' attributes unique, which is very useful after cloning. It updates 'for' attributes in 'label' tags as well. The 'for' and 'id' will continue to match and, if 'id' and 'name' are the same (common in form field elements and anchor tags). jQuery.fn.extend({ // Correct uniqueness for all id attributes and assoc labels. // Correct uniqueness for all name attributes. makeIdentifiersUnique: function(makeUnique) // makeUnique: (optional) returns unique identifier given an identifier (string) { var descendantOrSelf = this.find("*").andSelf(); var strUniqueSuffix = Math.floor(Math.random() * 1679616).toString(36); // 4 digit alphanum makeUnique = ("function" == typeof makeUnique ? makeUnique : function(id) { // Remove suffix of "_" 4-digit-alphanum (if it exists), then append new suffix return id.replace(/_[0-9a-z]{4}$/,"") + "_" + strUniqueSuffix; }); descendantOrSelf.filter("[id]").each(function() { this.id = makeUnique(this.id); }); descendantOrSelf.filter("label").each(function() { this.htmlFor = makeUnique(this.htmlFor); }); // caution: do not split id & name uniqueness code b/c for <a>, id should equal name, // which won't be the case if the .random() function is called twice. descendantOrSelf.filter("[name]").each(function() { try { if (jQuery.browser.msie) { // Microsoft JScript allows the name to be changed at run time. // HOWEVER! // This does not cause the name in the programming model to change // in the collection of elements, but it does change the name used // for submitting elements. The NAME attribute cannot be set at run time // on elements dynamically created with the createElement method. // To create an element with a name attribute, include the attribute // and value when using the createElement method. var strHTML = this.outerHTML + ""; strHTML = strHTML.replace(new RegExp("name=" + this.name, "g"), "name=" + makeUnique(this.name)); jQuery(this).replaceWith(strHTML); } else { this.name = makeUnique(this.name); } } catch (ex) { // ignore }; }); return this; } }); Please consider adding this to the jQuery library.
This code snippet, when added to the clone() method copies form field values. Tested with IE 7 and FF 2 on Windows. // Copy .value and .checked attributes of form fields var dstFormElements = ret.find("*").andSelf().filter(":input"); if (dstFormElements.length > 0) { var srcFormElements = this.find("*").andSelf().filter(":input"); dstFormElements.each(function(i) { $ektron(this).val( srcFormElements.eq(i).val() ); if ("checkbox" == this.type || "radio" == this.type) { this.checked = srcFormElements.get(i).checked; } }); } Please consider adding it to the clone method in the next release of jQuery.
When testing performance of several JavaScript libraries, I came across an unexpected result with the trim() method. The jQuery library (as well as several others) trim using a single reg exp replace. (text || "").replace( /^\s+|\s+$/g, "" ) This seems reasonable. Why process the string twice when once will do? However, using two reg expressions is signifanctly faster. That is, each regular expression is very simple (no '|') and therefore runs much faster, making up for processing the string twice. (text ? text.replace(/^\s+/g,"").replace(/\s+$/g,"") : "") Timing was done using Firebug (Firebug Lite for IE) on a Windows XP machine w/ Firebug 2 and IE 7. The test page is included here. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>JavaScript Library Test of 'trim'</title> <script type="text/javascript" src="/jsunit/app/jsUnitCore.js"></ script> <script type="text/javascript" src="/firebug/firebug.js"></ script> <script type="text/javascript" src="/jquery-1.2.1.js"></script> <script type="text/javascript"> <!-- function init() { if (console.open) console.open(); console.log("Initialized"); timeTrim("jQuery"); timeTrim("ektron"); } function timeTrim(name) { var testloop = 1000; var sNoSpace = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; var sEndSpace = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. "; var sBothSpace = " Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. "; var s0 = ""; var s1 = ""; var s2 = ""; console.time("trim: " + name); switch (name) { case "jQuery": for (var i = 0; i < testloop; i++) { s0 = $.trim(sNoSpace); s1 = $.trim(sEndSpace); s2 = $.trim(sBothSpace); } break; case "ektron": for (var i = 0; i < testloop; i++) { s0 = sNoSpace.replace(/^\s+/,"").replace(/\s+ $/,""); s1 = sEndSpace.replace(/^\s+/,"").replace(/\s+ $/,""); s2 = sBothSpace.replace(/^\s+/,"").replace(/\s+ $/,""); } break; } console.timeEnd("trim: " + name); if (s1 != s0) console.error(name + ": trim() failed w/ trailing space."); if (s2 != s0) console.error(name + ": trim() failed w/ spaces on both ends."); } // --> </script> </head> <body> <h2>JavaScript Library Test of 'trim'</h2> <script type="text/javascript"> <!-- jQuery(init); // --> </script> </body> </html>