简体   繁体   English

JavaScript和CSS在ASPX页面中的放置

[英]Placement of JavaScript and CSS in ASPX page

Can anyone suggest where should I put my JavaScript and CSS code in an ASPX page? 谁能建议我将JavaScript和CSS代码放在ASPX页面的什么位置? In the beginning or at the end? 在开始还是结束? Does wrong placement of JavaScript and CSS affect the optimization of page? JavaScript和CSS的错误放置是否会影响页面的优化?

Its better if you have both of them in separate files and then include them in your aspx page. 如果将它们都放在单独的文件中,然后将它们包含在aspx页中,则效果更好。 This way it will enable you to use the same CSS and Javascript on multiple pages. 这样,您就可以在多个页面上使用相同的CSS和Javascript。

But if you are looking to use the CSS and Javascript in only a single page then You should put your css in head tag, You can put javascript between head tag or any where between html but best place to put javascript is between head tags. 但是,如果您只想在单个页面中使用CSS和Javascript,则应将CSS放在head标签中。可以将javascript放在head标签之间或html之间的任何位置,但最好的放置JavaScript的地方是head标签之间。 Its better to put js like jquery libraries file in head. 最好将js之类的jquery库文件放在头部。

Putting your CSS at the top of the page and JavaScript at the bottom of the page( before the closing body tag ) is the best way when you want the CSS and Scripts in the same page. 当您希望CSS和Scripts位于同一页面时,将CSS放在页面顶部,将JavaScript放在页面底部( 在body标记之前 )是最好的方法。

You can have a read at this article( the stated points are explained in the middle sections )- 您可以阅读本文( 陈述的要点在中间部分中进行了说明 )-

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

The reason why JavaScript should be at the bottom is that any page elements that you access in your JavaScript should be loaded first in the DOM . JavaScript应该位于底部的原因是,您在JavaScript中访问的所有页面元素都应首先加载到DOM If you write them in the <head> section, it is likely that the JavaScript may fail to find the elements in the HTML and report errors or fail to execute. 如果在<head>部分中编写它们,则JavaScript可能无法在HTML中找到元素并报告错误或无法执行。

I would suggest to 我建议

  1. put stylesheet calls to the top of the <HEAD> element and 将样式表调用放在<HEAD>元素的顶部,然后
  2. scripts at bottom of the page that is after </body> tag. 页面底部</body>标记之后的脚本。 (In some cases) (在某些情况下)

Becuase that will make your web pages load faster. 因为这将使您的网页加载更快。

Note: 注意:

The second point will not always be true because in some cases you want your scripts to execute part of the content with the help of these scripts. 第二点并不总是正确的,因为在某些情况下,您希望脚本在这些脚本的帮助下执行部分内容。

So YES , the wrong placement of javascript and css affect the optimization of page 因此, 是的 ,JavaScript和CSS的错误放置会影响页面的优化

I would like to add one point: 我想补充一点:

Scripts should be kept at bottom of the page, eg after </body> tag provided page loading is not dependent upon that script. 脚本应保存在页面底部,例如,在</body>标记之后,前提是页面加载不依赖于该脚本。

Note: Placing scripts at the bottom of the page improves display speed, as script compilation slows down the display. 注意:将脚本放在页面底部可以提高显示速度,因为脚本编译会降低显示速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM