DWQA QuestionsCategory: ProgramJS Question: How do you understand that a page has two script tags?
bell tower and drum tower asked 2 weeks ago
<script>alert(a)</script>//error reporting: A is not defined
< script > var a = 12; alert (a) </script >// but did not stop executing after reporting an error, playing 12

If you change your position

<script>var a=12;alert(a)</script>
<script>alert(a)</script>//Code is normal

How do I understand the two script tags? Why did the first script report a mistake, but the program could go down and execute the second script tag? This is different from when writing JS code in real time: error reporting – stop execution? Thank you!

<script>alert(a)</script>//error reporting: A is not defined
< script > var a = 12; alert (a) </script >// but did not stop executing after reporting an error, playing 12
12 Answers
Best Answer
Dream meditation answered 2 weeks ago

When the JavaScript interpreter executes the script, it executes by block. Generally speaking, when browsers parse HTML document streams, if they encounter a < script > tag, the JavaScript interpreter will wait until the code block is loaded, precompile the code block, and then execute it. After execution, the browser continues to parse the following HTML document stream, while the JavaScript interpreter is ready to process the next block of code.
Since JavaScript is executed by block, calling variables or functions declared in the following block in a JavaScript block will prompt for grammatical errors. This is the case with the first code.
Although JavaScript is executed by blocks, different blocks belong to the same global scope, that is to say, variables and functions between blocks can be shared.This is why the second piece of code can access a in the previous block.

bell tower and drum tower replied 2 weeks ago

Why didn’t the first script tag stop executing after the first script tag reported an error, and the program went on to the next script tag?<script>alert(a)</script>//error reporting: A is not defined< script > var a = 12; alert (a) </script >// but did not stop executing after reporting an error, playing 12

sky91 answered 2 weeks ago

From network resources:
Step 1. Read the first code block.
Step 2. Do grammatical analysis, report grammatical errors if there are errors (such as parentheses mismatch), and jump to step 5.
Step 3. Pre-compile var variables and function definitions (never error, because only correct declarations are parsed).
Step 4. Execute code snippets and report errors if there are errors (such as undefined variables).
Step 5. If there is another code segment, read in the next one and repeat step 2.
Step 6. End.
Detailed explanation of execution sequence of JavaScript operation mechanism
Explanation of JavaScript Running Mechanism: Event Loop

Border town answered 2 weeks ago

Viewer’s View

html<script>alert(a)</script>//error reporting: A is not defined
< script > var a = 12; alert (a) </script >// but did not stop executing after reporting an error, playing 12

If merged

html<script>
Alert (a); // play undefined
var a = 12;
Alert (a); // bomb 12
</script>

This is equivalent to

html<script>
var a;
alert(a);
a = 12;
alert(a);
</script>

So the effect after merger is different from that before merger.
Finally, explain why the effect is different before and after the merger… I guess:
<script>Each tag loaded explains the code to execute, so only the first paragraph is loaded.<script>At that time, it has not been declared yet.aWrong report.
After the merge, all the code is loaded. According to the JavaScript specification, the declaration is pre-positioned, sovar aIf it is put in the front, it will not be wrong.

sanae answered 2 weeks ago

The code in each script tag is executed separately.
So the second will still be executed after an error is reported in the first tag.
The change is normal because the code for both tags is in global scope.
After the first tag declares variable a, the second tag finds it.

qianjiahao answered 2 weeks ago

Script just divides the execution environment of js, in fact, it can be seen as the JS execution environment of several segments.
In addition, the first type of error reporting is undefined because you use a when you do not assign a value.

Oyster Oil Beef Cover Pour Rice answered 2 weeks ago

You can understand that the browser merged the JavaScript code at both ends.

<script>
var a=12;
alert(a);
alert(a)
</script>

@ Border City MadmanI merged the second paragraph of the title code.