简体   繁体   English

以HTML格式导入的CSS和JS文件组?

[英]Group of CSS and JS files import at HTML?

I have a group of CSS imports as like: 我有一组CSS导入,例如:

<link rel="stylesheet" href="/css/reset.css"/>
<link rel="stylesheet" href="/css/visualize.css"/>
<link rel="stylesheet" href="/css/datatables.css"/>

and some JavaScript code imports as like: 和一些JavaScript代码导入,例如:

<script src="/js/excanvas.js"></script>
<script src="/js/jquery.js"></script>
<script src="/js/jquery.livesearch.js"></script>
<script src="/js/jquery.visualize.js"></script>

Is it possible to put all CSS import lines into a file ie cssImports.css and put all JS import lines into a file ie jsImports.js . 是否可以将所有CSS导入行放入文件即cssImports.css并将所有JS导入行放入文件即jsImports.js So when I want to import that CSS and JS group files I will write something like: 因此,当我想导入该CSS和JS组文件时,我将编写如下内容:

<link rel="stylesheet" href="/css/cssImports.css"/>
<script src="/js/jsImports.js"></script>

so all the files listed above will be imported? 所以上面列出的所有文件都将导入?

PS: I don't want to write any code belongs to web server specific. PS:我不想编写任何特定于Web服务器的代码。

Javascript imports: no. Javascript导入:否。 CSS import: yes, but you shouldn't because it breaks parallel downloading of stylesheets. CSS导入:是的,但是您不应该这样做,因为它会中断样式表的并行下载。

Your best bet is to use a local build script (such as the Ant script included with the HTML5 Boilerplate ) to concatenate your stylesheets and scripts before uploading them to the server, then linking to the 'master' resources in your HTML: 最好的选择是使用本地构建脚本(例如HTML5 Boilerplate附带的Ant脚本)来连接样式表和脚本,然后再将其上载到服务器,然后链接到HTML中的“主”资源:

<link rel="stylesheet" href="/css/master.css">
<script src="/js/master.js"></script>

There is a tutorial on using the Ant script . 有一个有关使用Ant脚本教程

Go with LazyLoad! 与LazyLoad一起使用! https://github.com/rgrove/lazyload/ It's a very small js (less than 1kb) that takes care of resource loading for you. https://github.com/rgrove/lazyload/这是一个非常小的js(小于1kb),可以为您处理资源加载。

Download the package and save on your js folder. 下载该软件包并保存在您的js文件夹中。 Then you would probably want to do this: 然后,您可能想要这样做:

<script src="js/lazyload-min.js"></script>

Then for javascript files: 然后对于javascript文件:

 <script>
LazyLoad.js(["/js/excanvas.js", "/js/jquery.js", "/js/jquery.livesearch.js", "/js/jquery.visualize.js"], function () {

      alert('all js files have been loaded');


    });
</script>

Css: CSS:

<script>
LazyLoad.css(["/css/reset.css", "/css/visualize.css", "/css/datatables.css"], function () {

      alert('all css files have been loaded');

    });
</script>

This will also boost the performance of your page, enabling parallel css and js loading (the latter on firefox opera only). 这也将提高页面的性能,启用并行的CSS和JS加载(仅后者在firefox Opera中)。

for css: 对于CSS:

<style>
    @import url('/css/styles.css');
</style>

for js you could try something like 对于js,您可以尝试类似

document.write("<script type='text/javascript' src='otherScript.js'></script>");

but i dont see a reason to do either of theese... 但我看不出有理由做任何一种...

You can Import CSS like this: 您可以这样导入CSS:

Create a new CSS cssImports.css and add there lines 创建一个新的CSS cssImports.css并在其中添加行

@import url('/css/reset.css');
@import url('/css/visualize.css');
@import url('/css/datatables.css');

and relate it in your homepage as: 并将其在您的主页中关联为:

<link rel="stylesheet" href="/css/cssImports.css"/>

For Javascript import doesn't work. 对于Javascript导入不起作用。 But you can create a single JS file and include the javascript code of each file after one another. 但是您可以创建一个JS文件,然后依次包含每个文件的javascript代码。 But this is not recommended. 但是不建议这样做。 It is better to have separate <script> tag for each js file. 最好为每个js文件使用单独的<script>标记。

This is now possible as follows with HTML Imports which are in W3C draft 现在可以通过W3C草案中的 HTML导入进行以下操作

<link rel="import" href="import.html">

import.html import.html

<link rel="stylesheet" href="/css/reset.css"/>
<link rel="stylesheet" href="/css/visualize.css"/>
<link rel="stylesheet" href="/css/datatables.css"/>
<script src="/js/excanvas.js"></script>
<script src="/js/jquery.js"></script>
<script src="/js/jquery.livesearch.js"></script>
<script src="/js/jquery.visualize.js"></script>

At this time only Chrome, Android and Opera support HTML Imports natively, but WebComponents provides a very mature polyfill script called webcomponents-lite.js to support all modern browsers 目前,只有Chrome,Android和Opera本身支持 HTML导入,但是WebComponents提供了非常成熟的polyfill脚本,称为webcomponents-lite.js,以支持所有现代浏览器

Yes just copy all the code and place in into a new file in the order than you would like it to run. 是的,只需将所有代码复制并按您希望运行的顺序放入新文件中即可。

I know there are some javascript libraries that can do this for you but I dont have an experience of using them. 我知道有一些javascript库可以为您完成此操作,但是我没有使用它们的经验。 I think Yahoo compiler/ YUI has one. 我认为Yahoo编译器/ YUI有一个。

I'm not recommend do that because performance issue, but if you want the way, you can do that: 由于性能问题,我不建议您这样做,但是如果您愿意,可以这样做:

For CSS yes its possible, in cssImports.css you can put: 对于CSS,可以在cssImports.css中输入:

@import url(/css/reset.css);
@import url(/css/visualize.css);
@import url(/css/datatables.css);

But for JS, I think no way as much as CSS, but you can do this (adding JS files) from one JS file (ex jsImports.js), by write code create script element and add this element to page, like that : 但是对于JS,我认为没有CSS那么多,但是您可以通过编写代码create script元素并将此元素添加到页面中,从一个JS文件(例如jsImports.js)中完成此操作(添加JS文件),如下所示:

var jsE = document.createElement('script');
var url = 'JS LINK HERE';
jsE.setAttribute('type', 'text/javascript');
jsE.setAttribute('src', url);
document.getElementsByTagName('head').item(0).appendChild(jsE);

Do this for each link of JS that you want to put, I have and idea, using Arracy contains JS links like this: 我想知道的是,对每个要放置的JS链接执行此操作,使用Arracy包含如下JS链接:

var jsLinks = new Array(
"/js/excanvas.js",
"/js/jquery.js",
"/js/jquery.livesearch.js",
"/js/jquery.visualize.js"
);

then a loop read a link each time and put this, like : 然后循环每次读取一个链接并将其放入,例如:

for (i = 0; i < jsLinks.length; i++)
{
    var jsE = document.createElement('script');
    var url = jsLinks[i];
    jsE.setAttribute('type', 'text/javascript');
    jsE.setAttribute('src', url);
    document.getElementsByTagName('head').item(0).appendChild(jsE);
}

I didn't test my code, But I hope my idea is explained well. 我没有测试我的代码,但是我希望我的想法能很好地解释。

Best 最好

Edit 1: yes you can use Gatekeeper solution for JS (Very Simple), but it use "write" but "for me" I don't like that way :) 编辑1:是的,您可以使用JS的Gatekeeper解决方案(非常简单),但是它使用“写”但“对我来说”,我不喜欢这种方式:)

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

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