开发者

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

Is there 开发者_JAVA技巧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>


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜