Created
April 21, 2011 16:26
-
-
Save joeegan/934916 to your computer and use it in GitHub Desktop.
throwErrorIfJquerySelectorUndefined.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // anonymous func to avoid polluting global namespace, and $ conflict | |
| (function($){ | |
| // throw error for undefined selectors | |
| var jQueryInit = $.fn.init; | |
| $.fn.init = function(selector, context, rootjQuery) { | |
| var result = new jQueryInit(selector, context, rootjQuery); | |
| if (result.length === 0) | |
| throw "jQuery cannot find the following selector: '" + selector + "'"; | |
| return result; | |
| }; | |
| // USAGE | |
| // if anywhere is you code you try to find a selector with jQuery that is not found, like so: | |
| // $('undefinedselector'); | |
| // you will get an error thrown in the console | |
| })(window.jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment