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>
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>
Share
Improve this question
asked Jan 25, 2011 at 1:42
NazmulNazmul
7,21812 gold badges53 silver badges64 bronze badges
2
- 1 See this: stackoverflow.com/questions/3735406/… – Šime Vidas Commented Jan 25, 2011 at 1:47
- @Šime Vidas, good reference.Helped me to learn new things. – Nazmul Commented Jan 25, 2011 at 2:32
2 Answers
Reset to default 16Functions 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.
Only performance difference. One block is slightly faster, but the code is the same.