-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsafeQuery.js
More file actions
32 lines (27 loc) · 940 Bytes
/
Copy pathsafeQuery.js
File metadata and controls
32 lines (27 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* https://github.com/ReallyGood/safeQuery */
(function($, w, c){
if(!w.jQuery || !c) return;
c.w = c.warn || c.log; // safely use either warn or log
// jQuery and console.warn/log are available, we're good to go
var _find = $.find,
_attr = $.fn.attr;
// 'duck punch' jQuery.find - Replace with a wrapper function with our warning which returns the original result
$.find = function(){
var result = _find.apply(this, arguments);
if(!result.length) c.w('jQuery Selector "' + result.selector + '" returned no matches');
return result;
};
$.fn.attr = function(attr){
if(arguments.length === 1) {
var result = _attr.call(this, attr);
if(result === void 0) {
c.w('jQuery Attribute Getter for "' + attr + '" returned undefined for selector "' + $(this).selector + '"');
}
return result;
} else {
_attr.apply(this, arguments);
return this;
}
};
w.jQuery = w.$;
}(jQuery, window, console));