Javascript not compatible with jQuery v1.11 or below -
i want write small script invert black , white colors on area of page. used inverting script found on thread , adjusted invert black , white , on area of page: http://jsfiddle.net/yqe9t/87/
this works fine on jquery 1.12.1 , above ive noticed of pages im working still use jquery 1.9 , reason script not work on there; http://jsfiddle.net/yqe9t/88/ (jquery 1.9 here). i'm unable change jquery version used on pages need make compatible that.
can please me figure out how make code work on older versions well? i'd work on new , old dont know enough javascript myself fix it.
my complete javascript:
$(".invertall").click (function () { var body = $(".unit.size-col-d.width610"); invertelementcolors ( $(body) ); } ); function rgb2hex(rgb){ rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i); return (rgb && rgb.length === 4) ? "#" + ("0" + parseint(rgb[1],10).tostring(16)).slice(-2) + ("0" + parseint(rgb[2],10).tostring(16)).slice(-2) + ("0" + parseint(rgb[3],10).tostring(16)).slice(-2) : ''; } function invertelementcolors (jnode) { jnode.children().each(function () { invertelementcolors ( $(this) ); }); jnode.css ( { 'color' : function (j, oldcolor) { return invertrgb_colorstr (oldcolor); }, 'background-color' : function (j, oldcolor) { return invertrgb_colorstr (oldcolor); } } ); } function invertrgb_colorstr (oldcolorstr) { // convert color rgb hex code can detect colors var = oldcolorstr; var colorhex = rgb2hex(help); // convert black , white if ((colorhex == '#000000') || (colorhex == '#ffffff')) { //--- special case if (oldcolorstr == 'transparent') oldcolorstr = 'rgb(255, 255, 255)'; //--- color text in rgb format. eg: rgb(1, 22, 255) var colorarray = oldcolorstr.match (/\((\d+),\s?(\d+),\s?(\d+)\)/); var newcolorstr = $.map (colorarray, function (byte, j) { if (!j) return null; //--- invert decimal byte. return math.abs (255 - parseint (byte) ); } ).join (','); return 'rgb(' + newcolorstr + ')'; } else { return oldcolorstr; } }
the source map
in 2 jquery version different.
one checking if value mapping on array before trying access property .length
.
map: function( elems, callback, arg ) { var length, value, = 0, ret = []; // go through array, translating each of items new values if ( isarraylike( elems ) ) { length = elems.length;
the failing not:
map: function( elems, callback, arg ) { var value, = 0, length = elems.length, isarray = isarraylike( elems ), ret = [];
so when regex fails, pass map null
accessing property .length
of null throws exception.
so solution if cannot upgrade jquery self, pass value map if oldcolorstr.match (/\((\d+),\s?(\d+),\s?(\d+)\)/);
doesn't return null
. stop exception.
Comments
Post a Comment