Why inline JavaScript is bad?

前端 未结 9 847
梦谈多话
梦谈多话 2020-11-29 08:13

It is always recommended to avoid inline Javascript codes by putting all codes in a JS file, which is included in all pages. I wonder, if this does not cause pe

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 08:52

    there are variuos case that needs to be keep in mind while placing js code.

    For inline :

    1. there is no need to navigate to an external file if you need to change something quickly, so its better in locality

    2. if you are using AJAX in some elements of your page you may loose all the dom element onclick etc for that section, that obviously depends on how you binded them. for ex you can use live or delegate in case your using jQuery to avoid above said problem... but i find that if js is small enough it is preferable to just put it inline.

    Now there other theory for ex

    Externalizing javascript is one of the yahoo performance rules:

    http://developer.yahoo.com/performance/rules.html#external

提交回复
热议问题