jquery - javascript if condition not working? -
i sitting hours in front of following code snippet , can not running want.
basically code creates navigation menu on right click, click on switcher should turn off function , on next click gets turned on again.
everything works fine (like expected), little if statement on line 12 ( if ( switcher % 2 == 0 ) ) not work expected, meaning code within gets executed whether var switcher or not even. tried other conditions "> 0" , on code within gets executed.
$(document).ready(function () { /* set switcher 0 */ switcher = 0; /* if switch gets clicked increment var switcher*/ $('#guidenavschalter').click(function () { switcher++; return false; }); /* if var switcher execute following code, if not nothing of this*/ if (switcher % 2 == 0) { /* not display right click browser menu */ document.oncontextmenu = function () { return false; }; /* if click within #page excluding area of #newid */ $('#page:not(#newid)').mousedown(function (e) { /* if right click */ if (e.button == 2) { /* if #newid exist display again */ if ($('#newid').length) { $('#newid').css({ "display": 'block' }); $('#newid').css({ "top": e.pagey + 'px' }); $('#newid').css({ "left": e.pagex + 'px' }); /* if not exist create , display #newid */ } else { var $div = $('#block-bookoblock-book-outline').clone().attr('id', 'newid'); $('body').append($div); $('#newid').css({ "top": e.pagey + 'px' }); $('#newid').css({ "left": e.pagex + 'px' }); $('#newid').css({ "position": 'absolute' }); return false; } } /* if left click hide #newid */ if (e.button == 0) { $('#newid').css({ "display": 'none' }); } return true; }); } });
i don't think condition switcher % 2 == 0
problematic here. have hooked events in case of true there else statement unhooks events original functionality resumed? i.e. right click create default context menu.
update:
in order resume original functionality, call
document.oncontextmenu = null;
in else part.
also, need define $('#page:not(#newid)').mousedown(function (e) {
once (outside of if/else ) , use switcher variable determine whether call functionality or not.
in short, need following
$(document).ready(function () { /* set switcher 0 */ switcher = 0; /* if switch gets clicked increment var switcher*/ $('#guidenavschalter').click(function () { switcher++; return false; }); document.oncontextmenu = function() { if ( switcher % 2 == 0 ) { return false; } else { return true; } }; /* if click within #page excluding area of #newid */ $('#page:not(#newid)').mousedown(function (e) { if (switcher % 2 == 0) { // stuff } else { // nothing } }); });
Comments
Post a Comment