How does the location of a script tag in a page affect a JavaScript function that is defined in it?

后端 未结 9 1249
死守一世寂寞
死守一世寂寞 2020-11-29 04:27

I read that you should define your JavaScript functions in the tag, but how does the location of the

9条回答
  •  情深已故
    2020-11-29 04:51

    Javascript's scoping rules are similar to perl - you can call any function at the current or any higher scope level. The only restriction is that the function has to be defined at the time you call it. The position in the source is irrelevant - only the position in time matters.

    You should avoid putting scripts in the if possible as it slows down page display (see the link Alan posted).

提交回复
热议问题