开发者

embedding <script> tags in body.. what are the implications?

I love to embed <scri开发者_如何转开发pt> tags in the <body> and its all over the place. This have the implication that the generated code becomes human-unreadable.. but my concern is.. other than that what other implications do we have?

Is it true that a browser has to do more "work" when we have like 300 generated stand-alone tags embedded all over the place within the tag?


Yes.

Each time the browser encounters a <script> tag, it needs to fire up a Javascript interpreter.

However, you shouldn't worry about that; you should use whatever approach is easiest to understand and debug.


You get a bigger chunk of text to send to the client every time someone requests your page. This is because normally pages aren't cached which means that if you request the same page several times, everything needs to be re-downloadeded from the server.

However if you on the other hand places all your JavaScript code in a seperate .js file you can easily cache, minify and gzip that file, which can make a huge difference in the end. The same is true for inline css vs css in an external file also.


If your page has static code within the <script> tags, consider placing such code within functions in a separate .js file with just calls to the functions in web pages.

Also, <script>s should also make use of the defer="defer" option if the script is not needed to render the page (e.g., the code within is only active after the page loads... such as code used in "on" stuff. This allows browsers to put off compiling the scripts within until after page load so you may get a slightly snappier response.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜