简体   繁体   English

处理 javascript 和 css 文件的最佳实践是什么

[英]What is best practice to handle javascript and css files

How do You manage all of Your .js and .css files in ASP.NET project?您如何管理 ASP.NET 项目中的所有.js.css文件? Especially when they have a lot of dependency between each other?尤其是当他们彼此之间有很多依赖关系时?

I've combined all script in one.我将所有脚本合二为一。 But it's become weighty, and 90% of them were not used on particular pages.但它变得很重要,其中 90% 没有在特定页面上使用。 What I want is an instrument or guidance to manage all of those scripts, simple dependency management, that help to include on page only those JS and CSS that needed on this page.我想要的是管理所有这些脚本的工具或指南,简单的依赖项管理,有助于在页面上仅包含此页面所需的那些 JS 和 CSS。

Also used ScriptManager nut when You use a lot of controls it's very handy.... maybe I'm using it in the wrong way.当您使用大量控件时,还使用了 ScriptManager 螺母,它非常方便......也许我以错误的方式使用它。

On our projects, we tag the scripts and the CSS as resources for the class, and then register them during the page lifecycle, usually in PreRender().在我们的项目中,我们将脚本和 CSS 标记为 class 的资源,然后在页面生命周期中注册它们,通常在 PreRender() 中。

For example:例如:

// Css
[assembly: WebResource("Assembly.Class.MyClass.css", "text/css")]
// Javascript
[assembly: WebResource("Assembly.Class.MyClass.js", "text/javascript")]
namespace OurNamespace
{
   public class MyClass...

We then set the properties of each of our scripts and css files to be Embedded Resources.然后,我们将每个脚本和 css 文件的属性设置为嵌入式资源。

This approach lets you keep your scripts seperate and targeted to individual UI components.这种方法使您可以将脚本分开并针对各个 UI 组件。 You can register the same resources to multiple classes, and then ScriptManager will take care of making sure that the right resources show up on the page.您可以将相同的资源注册到多个类,然后 ScriptManager 将负责确保正确的资源显示在页面上。

We then wrote a class at the HTTP handler level that handles compressing all the CSS resources into one file before it's streamed out, to make sure we didn't hit the 32 CSS file limit for IE6. We then wrote a class at the HTTP handler level that handles compressing all the CSS resources into one file before it's streamed out, to make sure we didn't hit the 32 CSS file limit for IE6. It also strips out whitespace, comments, etc. from our scripts to optimize the javascript output.它还从我们的脚本中去除空格、注释等,以优化 javascript output。

This is how I do it usually:这就是我通常的做法:

CSS: 5 files initially. CSS:最初有 5 个文件。 reset.css (from YUI), structure.css, general.css (borders, backgrounds, z-index etc), typography.css and base.css which imports the 4 other css files. reset.css (from YUI), structure.css, general.css (borders, backgrounds, z-index etc), typography.css and base.css which imports the 4 other css files.

Javascript: What I have done is taken the code behind idea of ASP.NET and applied it to my JS files in terms of naming. Javascript:我所做的是采用 ASP.NET 思想背后的代码并将其应用于我的 JS 文件的命名。 Example: page specific JS file for home.aspx is called home.aspx.js.示例:home.aspx 的页面特定 JS 文件称为 home.aspx.js。 Then I'll have separate JS files based on plugin or functionality and probably a common.js which will contain all the global vars.然后,我将拥有基于插件或功能的单独 JS 文件,可能还有一个包含所有全局变量的 common.js。

This may not be everyone's cup of tea, but I hope that gives you some ideas!这可能不是每个人的一杯茶,但我希望能给你一些想法!

I prefer to divide my JS files based on their function - for instance, I could have a single JS file for all AJAX based interaction, one for all Validations and a common JS library for all functions that are common to the entire web application.我更喜欢根据它们的 function 划分我的 JS 文件 - 例如,我可以为所有基于 AJAX 的交互创建一个 JS 文件,一个用于所有验证,一个用于整个 Z2567A6DZZ840 通用的所有函数的通用 JS 库。 Having a single file that combines the entire JS scripts into one would definitely slow down the application because each page would load the entire file, even though only a small portion might be relevant.拥有一个将整个 JS 脚本组合成一个文件的文件肯定会减慢应用程序的速度,因为每个页面都会加载整个文件,即使只有一小部分可能是相关的。

For CSS files, I prefer to have a single common stylesheet that would contain the general styles available to the entire application.对于 CSS 文件,我更喜欢使用单个通用样式表,其中包含可用于整个应用程序的通用 styles。 I might also create individual CSS files for pages that have a very specific layout structure.我还可以为具有非常特定布局结构的页面创建单独的 CSS 文件。

I don't know of any tools that could handle this dependency automatically, but When you divide your files according to function, this becomes unnecessary in most cases.我不知道有任何工具可以自动处理这种依赖关系,但是当您根据 function 划分文件时,在大多数情况下这变得不必要。

I divide JS files by its functionality.我按功能划分 JS 文件。

Most common functions that used almost everywhere go to one file,几乎无处不在的最常用功能 go 到一个文件,

Other classes and methods go to their own files.其他类和方法 go 到自己的文件中。

For CSS, I have one common file for the whole site.对于 CSS,我有一个用于整个站点的通用文件。

If I have sections that are visually different than others, I separate CSS files to per section.如果我的部分在视觉上与其他部分不同,我将 CSS 文件分隔到每个部分。 Also, I have a tabbed div control, it has a separate CSS file.另外,我有一个选项卡式div控件,它有一个单独的 CSS 文件。 I do not mix the files.我不混合文件。

For components, embedding resources look good, but sometimes it's good to fix bugs with only deploying JS files.对于组件来说,嵌入资源看起来不错,但有时只部署 JS 文件来修复 bug 也不错。

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

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