Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference in writing javascript in a single script block or multiple blocks

Tags:

javascript

Is there any difference in writing javascript in a single script block or in individual blocks?

Writing script in a single block

<script type="text/javascript">
function funcA(){
//do something
}

function funcB(){
//do something
}
</script>

Writing script in a different block

Block 1:

<script type="text/javascript">
function funcA(){
//do something
}
</script>

Block 2:

<script type="text/javascript">
function funcB(){
//do something
}
</script>
like image 916
Nazmul Avatar asked Jan 25 '11 01:01

Nazmul


People also ask

Is it OK to have multiple script tags 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. The <SCRIPT> tags are processed as they are encountered.

Can we call more than one JavaScript in a single line?

Answer. Yes, you can call multiple methods, one after the other in the same statement.

Why is it a good idea to put your JavaScript either with script tags or an external file link near the end of the HTML body instead of in the head of the HTML?

External JavaScript Advantages Placing scripts in external files has some advantages: It separates HTML and code. It makes HTML and JavaScript easier to read and maintain. Cached JavaScript files can speed up page loads.

Where is the best place to put JavaScript code to make it non blocking?

The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load. Generally, JavaScript code can go inside of the document <head> section in order to keep them contained and out of the main content of your HTML document.


1 Answers

Functions declared in an earlier script block can only call functions in a later script block after the page loads.

Also, if an error occurs while the first script block is executing, the second block will still run.
If you put it all in one script, any code after the error will not run at all. (except for function declarations)

All this only applies to code that runs immediately.
Code that runs later (eg, an event handler) will not be affected.

like image 174
SLaks Avatar answered Sep 27 '22 19:09

SLaks