-3

Why won't a JavaScript function run if there is an error in another function?

I ran this html page and tried to load the alert from the popup1() function, but it wouldn't work because there is an error in the if statement of the popup2() function:

<html>
<body>

<button onclick="popup1()"> Pop up 1 </button>
<button onclick="popup2()"> Pop up 2 </button>

<script>
function popup1()
{
    alert ("Pop up 1");
}

function popup2()
{
    if ( 1 = 1)
    {
        alert ("Pop up 2");
    }
}
</script>

</body>
</html>

When I corrected the if statement to if (1 == 1), both functions worked.

Why did this affect the other function?

Is there any free software you can recommend that will find syntax errors in JavaScript for me, I really don't want to trawl through code again because of a missing equal sign. I tried eclipse for php but it didn't manage to find this.

  • because errors block subsequent execution. The error is telling you something is wrong...so why should it keep going? – charlietfl Oct 08 '15 at 19:11

3 Answers3

3

Javascript runs in blocking sequence, so if there is any error anywhere it will stop execution.

(this is assuming you have no asynchronous function callbacks that started before the error happened)

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • 2
    Asking for software recommendations is against Stack Overflow question rules. Asking for why something happens is kosher (which is what I answered) – Naftali Oct 08 '15 at 19:12
  • @Pamblam yes, but those types of errors are _not usually_ blocking. (unless something is supposed to happen afterwards) – Naftali Oct 08 '15 at 19:13
  • This is not actually what is going on here. This is a parse error, not a runtime error. This is NOT about Javascript running - it is about Javascript parsing which is not what your answer describes. – jfriend00 Oct 08 '15 at 19:15
  • I said nothing about the _type_ of error @jfriend00 – Naftali Oct 08 '15 at 19:17
  • You says "Javascript **runs** in blocking sequence". This particular page never even runs. It fails at the parse stage. Your answer just doesn't not describe what is happening here. – jfriend00 Oct 08 '15 at 19:18
  • @jfriend00 sorry I dont type out my thoughts well. I can change to runs/parses in blocking sequence if you want. The OP just needs to know that errors in JS are **blocking** – Naftali Oct 08 '15 at 19:18
1

The line of code if ( 1 = 1) is a parse error in Javascript. When your code fails to parse properly, the Javascript parser stops parsing your code immediately and that script is considered to have a fatal error and is not loaded.

At that point, it has found illegal Javascript and at best the parser has decided this is a fatal error and at worst, the parser is hopelessly confused about what your code would have meant and cannot proceed. In any case, this is how the Javascript parser works. It stops on the first parse error it encounters within any given script.

Your specific error would have been shown to you in the Javascript console as soon as you loaded that page. Your FIRST line of defense should be to keep your eye on the debug console. You should watch it regular, but ALWAYS look there whenever anything is not working as you expect.

In Chrome, it would have told you:

Uncaught ReferenceError: Invalid left-hand side in assignment

In addition, you can run various "lint" type programs on your code and it will advise you not only about errors, but also about dangerous practices. Personally, I use http://jshint.com/, but there are several different programs that offer this capability.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Why the downvote? This is the only answer the explains what the error is, and that the parser stops on the first error so no other code in this script tag will be loaded and explains how the OP can see these errors for themselves. – jfriend00 Oct 08 '15 at 19:36
  • I didn't even realize I could use the console in the browser for this, it would have saved me a couple of hours of nonsense. And thank you for the simple explanation! – user3059460 Oct 08 '15 at 19:50
0

this error is because a number can not be redeclare, since a number always going to be the same number. This causes a syntax error and affects the rest of the code. if you try to make this example work without problems.

function popup2()
    {
        var number = 1;
        if ( number = 1)
        {
            alert ("Pop up 2");
        }
    }