Bug Tracker

Opened 10 years ago

Closed 10 years ago

#5589 closed enhancement (fixed)

$(undefined) selecting the whole document is confusing

Reported by: iamnoah Owned by:
Priority: major Milestone: 1.4
Component: unfiled Version: 1.3.2
Keywords: Cc:
Blocked by: Blocking:

Description

var mySelector; $(mySelector).click(function() {

window.location = "/foo.html";

});

The above code has a very unexpected and difficult to debug result if mySelector is not initialized or is initialized to null, 0 or "". No error is throw, and no warning is given but suddenly any click in the document will take the user to another page, which is very hard to debug. This could be easily solved by adding something like this to the $ function:

if(!selector && window.console && arguments.length >= 1) {

console.warn("You passed a null/undefined selector argument, the " +

"full document will be selected.");

}

This would not affect calls like $().click since they do not pass any arguments. The intent is to only warn the user if they pass a selector argument that is invalid.

To avoid checking in production code, the $ function could be wrapped on startup only if the console is present:

if(window.console) {

jQuery = window.jQuery = window.$ = (function() {

var orig = jQuery; return function(selector,context) {

if(!selector && arguments.length >= 1) {

console.warn("You passed a null/undefined selector argument, the " +

"full document will be selected.");

} return orig(selector,context);

};

})();

}

Change History (1)

comment:1 Changed 10 years ago by dmethvin

Resolution: fixed
Status: newclosed

This is fixed in the nightlies and will be in 1.4.

Note: See TracTickets for help on using tickets.