组织外部JavaScript文件的好方法是什么? [英] What's a good way to organize external JavaScript file(s)?

查看:77
本文介绍了组织外部JavaScript文件的好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在具有大量HTML页面的ASP.NET Web应用程序中,许多内联JavaScript函数正在积累。将它们组织成外部文件的好计划是什么?大多数函数特定于编写它们的页面,但有些函数与整个应用程序相关。

In an ASP.NET web application with a lot of HTML pages, a lot of inline JavaScript functions are accumulating. What is a good plan for organizing them into external files? Most of the functions are particular to the page for which they are written, but a few are relevant to the entire application.

单个文件可能会变得非常大。使用C#等,我通常将文件至少划分为包含一般函数和类的文件,以便我可以将同一文件用于其他应用程序,并将一个文件用于特定于此应用程序的函数和类。但是,我不认为大文件会对Web应用程序的性能有好处。

A single file could get quite large. With C#, etc., I usually divide the files at least into one containing the general functions and classes, so that I can use the same file for other applications, and one for functions and classes particular to this application. I don't think that a large file would be good for performance in a web application, however.

这方面的想法是什么?

推荐答案

您可能希望每个页面在一个位置具有特定于页面的JavaScript,然后在大文件中包含所有共享JavaScript。如果SRC是大文件,那么用户的浏览器将在第一次加载时缓存JavaScript代码,文件大小不会成为问题。如果您特别担心它,可以将JavaScript源打包/缩小为可分发形式,并节省几千字节。

You probably want each page to have its page-specific JavaScript in one place, and then all the shared JavaScript in a large file. If you SRC the large file, then your users' browsers will cache the JavaScript code on the first load, and the file size won't be an issue. If you're particularly worried about it, you can pack/minify your JavaScript source into a "distributable" form and save a few kilobytes.

这篇关于组织外部JavaScript文件的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆