包括单独文件中的所有脚本。

时间:2021-12-06 02:02:07

Making a site and I have a number of scripts piling up. I was thinking to myself it might be better to have a separate file to 'include' all my scripts without the clutter in my main html code.

做一个网站,我有很多脚本堆叠。我在想,最好有一个单独的文件来“包含”所有的脚本,而不是主要的html代码中的混乱。

Is this possible? What are some of the styles / methods people use for such a thing?

这是可能的吗?人们用什么方式来做这样的事情?

2 个解决方案

#1


3  

browserify, webpack and requirejs are currently some popular bundling libraries.

browserify、webpack和requirejs目前是一些流行的捆绑库。

Alternatively, you can simply concatenate all of your scripts into one file either manually using something like the cat command in your terminal, or automatically, by using npm or a task runner like grunt or gulp then just include that file in your html. The benefit of using something like npm, gulp or grunt is that you can automatically watch your files for any changes, and when changes occur, it will concatenate all your various javascript files together for you.

或者,您可以简单地将所有脚本连接到一个文件中,或者手动地使用终端中的cat命令,或者自动地使用npm或像grunt或gulp这样的任务运行程序,然后将该文件包含在html中。使用npm、gulp或grunt之类的东西的好处是,您可以自动监视您的文件以进行任何更改,当发生更改时,它会将您的各种javascript文件连接到一起。

#2


1  

You may want to look into using HTML5 Imports using web components (http://webcomponents.org/) and polymer (https://www.polymer-project.org/1.0/), html imports and polyfill are native in chrome and then use simply use provided scripts for browser compatibility in IE and FF.

您可能希望使用web组件(http://webcomponents.org/)和聚合体(https://www.聚合-project.org/1.0/)来使用HTML5导入,html导入和polyfill在chrome中是原生的,然后使用简单的脚本为IE和FF的浏览器兼容性提供脚本。

#1


3  

browserify, webpack and requirejs are currently some popular bundling libraries.

browserify、webpack和requirejs目前是一些流行的捆绑库。

Alternatively, you can simply concatenate all of your scripts into one file either manually using something like the cat command in your terminal, or automatically, by using npm or a task runner like grunt or gulp then just include that file in your html. The benefit of using something like npm, gulp or grunt is that you can automatically watch your files for any changes, and when changes occur, it will concatenate all your various javascript files together for you.

或者,您可以简单地将所有脚本连接到一个文件中,或者手动地使用终端中的cat命令,或者自动地使用npm或像grunt或gulp这样的任务运行程序,然后将该文件包含在html中。使用npm、gulp或grunt之类的东西的好处是,您可以自动监视您的文件以进行任何更改,当发生更改时,它会将您的各种javascript文件连接到一起。

#2


1  

You may want to look into using HTML5 Imports using web components (http://webcomponents.org/) and polymer (https://www.polymer-project.org/1.0/), html imports and polyfill are native in chrome and then use simply use provided scripts for browser compatibility in IE and FF.

您可能希望使用web组件(http://webcomponents.org/)和聚合体(https://www.聚合-project.org/1.0/)来使用HTML5导入,html导入和polyfill在chrome中是原生的,然后使用简单的脚本为IE和FF的浏览器兼容性提供脚本。