Skip to main content

Bug Tracker

Side navigation

#5589 closed enhancement (fixed)

Opened December 03, 2009 02:41PM UTC

Closed December 03, 2009 05:10PM UTC

$(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);

};

})();

}

Attachments (0)
Change History (1)

Changed December 03, 2009 05:10PM UTC by dmethvin comment:1

resolution: → fixed
status: newclosed

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