Nazmul
Nazmul

Reputation: 7218

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

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>

Upvotes: 11

Views: 13293

Answers (2)

SLaks
SLaks

Reputation: 887469

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.

Upvotes: 16

Mano Kovacs
Mano Kovacs

Reputation: 1514

Only performance difference. One block is slightly faster, but the code is the same.

Upvotes: 0

Related Questions