From 78f402b164a273b9df9104728c129bc2fd090e46 Mon Sep 17 00:00:00 2001 From: chrisheijnen Date: Fri, 26 Sep 2014 09:41:41 +0200 Subject: [PATCH] Check for attr coords on the area's This extra check is build in before looping through the area's because if it has an area without the attribute 'coords', then the code will break. --- jquery.rwdImageMaps.js | 104 +++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/jquery.rwdImageMaps.js b/jquery.rwdImageMaps.js index c9d7348..a15fce1 100644 --- a/jquery.rwdImageMaps.js +++ b/jquery.rwdImageMaps.js @@ -8,60 +8,62 @@ * http://mattstow.com * Licensed under the MIT license */ -;(function($) { - $.fn.rwdImageMaps = function() { - var $img = this; - - var rwdImageMap = function() { - $img.each(function() { - if (typeof($(this).attr('usemap')) == 'undefined') - return; - - var that = this, +; (function ($) { + $.fn.rwdImageMaps = function () { + var $img = this; + + var rwdImageMap = function () { + $img.each(function () { + if (typeof ($(this).attr('usemap')) == 'undefined') + return; + + var that = this, $that = $(that); - - // Since WebKit doesn't know the height until after the image has loaded, perform everything in an onload copy - $('').load(function() { - var attrW = 'width', + + // Since WebKit doesn't know the height until after the image has loaded, perform everything in an onload copy + $('').load(function () { + var attrW = 'width', attrH = 'height', w = $that.attr(attrW), h = $that.attr(attrH); - - if (!w || !h) { - var temp = new Image(); - temp.src = $that.attr('src'); - if (!w) - w = temp.width; - if (!h) - h = temp.height; - } - - var wPercent = $that.width()/100, - hPercent = $that.height()/100, + + if (!w || !h) { + var temp = new Image(); + temp.src = $that.attr('src'); + if (!w) + w = temp.width; + if (!h) + h = temp.height; + } + + var wPercent = $that.width() / 100, + hPercent = $that.height() / 100, map = $that.attr('usemap').replace('#', ''), c = 'coords'; - - $('map[name="' + map + '"]').find('area').each(function() { - var $this = $(this); - if (!$this.data(c)) - $this.data(c, $this.attr(c)); - - var coords = $this.data(c).split(','), - coordsPercent = new Array(coords.length); - - for (var i = 0; i < coordsPercent.length; ++i) { - if (i % 2 === 0) - coordsPercent[i] = parseInt(((coords[i]/w)*100)*wPercent); - else - coordsPercent[i] = parseInt(((coords[i]/h)*100)*hPercent); - } - $this.attr(c, coordsPercent.toString()); - }); - }).attr('src', $that.attr('src')); - }); - }; - $(window).resize(rwdImageMap).trigger('resize'); - - return this; - }; -})(jQuery); \ No newline at end of file + + $('map[name="' + map + '"]').find('area').each(function () { + if ($(this).attr('coords')) { + var $this = $(this); + if (!$this.data(c)) + $this.data(c, $this.attr(c)); + + var coords = $this.data(c).split(','), + coordsPercent = new Array(coords.length); + + for (var i = 0; i < coordsPercent.length; ++i) { + if (i % 2 === 0) + coordsPercent[i] = parseInt(((coords[i] / w) * 100) * wPercent); + else + coordsPercent[i] = parseInt(((coords[i] / h) * 100) * hPercent); + } + $this.attr(c, coordsPercent.toString()); + } + }); + }).attr('src', $that.attr('src')); + }); + }; + $(window).resize(rwdImageMap).trigger('resize'); + + return this; + }; +})(jQuery);