Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does "scope" work with multiple script tags for Javascript in an HTML document?

Tags:

I'm not quite sure what goes on between <script> tags. For example, the following gives a reference error and type error in Chrome:

<html>     <head>     <script type="text/javascript">         T.prototype.test = function() {             document.write("a");         }     </script>     <script type="text/javascript">         function T() {}         var v = new T();         v.test();     </script>     </head>     <body>     </body> </html> 

But this works:

<html>     <head>     <script type="text/javascript">     </script>     <script type="text/javascript">         T.prototype.test = function() {             document.write("a");         }         function T() {}         var v = new T();         v.test();     </script>     </head>     <body>     </body> </html> 
like image 213
mshang Avatar asked Nov 19 '11 04:11

mshang


People also ask

Can you use multiple script tags in HTML?

An HTML page can contain multiple <script> tags in the <head> or <body> tag. The browser executes all the script tags, starting from the first script tag from the beginning.

How many script tags can you have in HTML?

Multiple <SCRIPT> Tags Up to this point all of the JavaScript Code was in one <SCRIPT> tag, this does not need to be the case. You can have as many <SCRIPT></SCRIPT> tags as you would like in a document.

What is script scope in JavaScript?

In JavaScript, scopes are created by code blocks, functions, modules. While const and let variables are scoped by code blocks, functions or modules, var variables are scoped only by functions or modules. Scopes can be nested. Inside an inner scope you can access the variables of an outer scope.

Does it matter where you put the script tag in HTML?

Learn HTML You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is normally recommended that you should keep it within the <head> tags. The <script> tag alerts the browser program to start interpreting all the text between these tags as a script.


2 Answers

The upper script is executed first in the first example, so it doesn't know about T yet, hence the error.

In the second example, T is well defined and known anywhere as soon as the script tag is executed. This is due to function declarations being hoisted to the top, no matter what the order is, they are always available. Function declaration hoisting is more deeply explained here

The second example after hoisting is applied:

var v,    T = function(){}; /* using comma like this is short-hand for: var v; var T = function(){}; */  T.prototype.test = function() {         document.write("a");     };  v = new T(); v.test(); 
like image 94
Esailija Avatar answered Sep 22 '22 15:09

Esailija


They are each parsed in the global context, but in order. The entire first script tag is parsed and executed before the second one is considered. As part of parsing a script, function declarations are recognized ahead of time, which is why the second one works while the first one doesn't.

like image 31
Jim Deville Avatar answered Sep 20 '22 15:09

Jim Deville