From 7431799c113df1ef96d0e397329195a9205a299e Mon Sep 17 00:00:00 2001 From: zPlus <-> Date: Mon, 27 Mar 2017 21:40:03 +0200 Subject: [PATCH] freepost.js: Do not bind "click" event to vote buttons if user is not logged in. --- javascript/freepost.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/javascript/freepost.js b/javascript/freepost.js index ee401d73..60e74b57 100644 --- a/javascript/freepost.js +++ b/javascript/freepost.js @@ -103,15 +103,20 @@ document.addEventListener ('DOMContentLoaded', function() { * - up arrow * - votes sum * - down arrow + * + * However, if the user is not logged in, there's only a text + * with the sum of votes, eg. "2 votes" (no children). */ let vote_sections = document.querySelectorAll ('.vote'); // Bind vote() event to up/down vote arrows for (let i = 0; i < vote_sections.length; i++) - { - vote_sections[i].children[0].addEventListener ('click', function () { vote ('up', this.parentNode) }); - vote_sections[i].children[2].addEventListener ('click', function () { vote ('down', this.parentNode) }); - } + // See comment above on the "vote_sections" declaration. + if (vote_sections[i].children.length > 0) + { + vote_sections[i].children[0].addEventListener ('click', function () { vote ('up', this.parentNode) }); + vote_sections[i].children[2].addEventListener ('click', function () { vote ('down', this.parentNode) }); + } // Bind onkeydown()/onkeyup() event to keys document.onkeydown = document.onkeyup = function(e) {