Suggestions for plugin JS
-
I’ve read through the JS the plugin uses and have a few suggestions.
jQuery('#sb_instagram.sbi').each(function(){This line here is bad, mainly because having multiple elements on the page with the same
idis not valid HTML, and what you’re doing here (looping through multiple elements with the sameid) isn’t technically speaking valid. Changing this to a class would make it valid, and enable users to use more than one of the widget on the page without generating invalid HTML.parseIntis used with only one parameter multiple times, some on user input values. IfparseIntreceives a string starting with0, it assumes the string is an octet and parses it as if it is base 8 unless10is passed as the second parameter,base. This is especially bad here because you’re usingparseIntto parseuserId, and if the user accidentally adds in a0in front of his ID he would find himself potentially loading someone else’s Instagram feed.jQuery(a).attr("data-date")should be withjQuery(a).data('date')– jQuery made some major changes to the wayattrworks in 1.6, so that the now most of the time when you useattr(), you really should be usingprop(), or in this case, since you’re usingdata-*attributes,data().$self.find('.sb_instagram_header .sbi_header_img_hover').fadeIn(200)– this could be changed to use pure CSS usingtransitioninstead of JavaScript, which is more complex and less performant..sbi_photo { transition: opacity 0.3s; } .sbi_photo:hover { opacity: 0.85; }return (Math.round(Math.random())-0.5);– usingMath.random()for shuffling isn’t a very good idea. The amount of actual shuffling that happens is strongly dependent on the browser’s implementation ofsort, and in most browsers would not produce an unbiased distribution. It’s not terribly important here, but changing it to the correct code is fairly trivial – see: http://stackoverflow.com/q/962802/313758
The topic ‘Suggestions for plugin JS’ is closed to new replies.