Question. Why the "Pancake" works when I press on one of these elements

document.onclick = function () { if (window.i == 1) { var ctrl = true; document.getElementById('logo_text2').onmousedown = function () { ctrl = false } document.getElementById('phrase_text').onmousedown = function () { ctrl = false } document.getElementById('phrase_author').onmousedown = function () { ctrl = false } if (ctrl != false) { alert('Блин') } } } 
  • one
    Well, because you most likely do not understand what you have written here. How do you think this code should be executed, what comes first, what then? - aachurin

3 answers 3

So at you ctrl is declared through var in functions (therefore - it is unavailable from the outside), and you try to reach it from third-party functions. Plus, onclick runs after onmousedown and has nothing to do with it.

    Remove the line if (ctrl! = False) {alert ('Damn') And everything will be fine.

    • And we get all the same "unexpected" triggering of document.onclick every time - aachurin
    • "Damn" it will not work .. - D7na

    In short, it is necessary so:

     var ctrl = true; function ctrlf(){ ctrl=false; alert('Блин'); } document.getElementById('logo_text2').onmousedown = ctrlf; document.getElementById('phrase_text').onmousedown = ctrlf; document.getElementById('phrase_author').onmousedown = ctrlf; 

    Do I understand the essence?