1

I have many .js files loaded in my index.html in the following format:

<script src="file.js"></script>

Is there an option to create an HTML file where I will source all my JS and CSS files and then include this HTML file in my index.html?

Adam Azad
  • 11,171
  • 5
  • 29
  • 70
Misha Groiser
  • 133
  • 11

2 Answers2

0

In order to load a HTML file, add a link tag with an import in the rel attribute and an href that contains a path to the HTML file. For example, if you want to load a HTML file called myImports.html into index.html:

index.html

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

You can load any resource including scripts, stylesheets, and web fonts, into the imported HTML just like you do to regular HTML files:

myImports.html

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

Whereas doctype, html, head, body aren't required. HTML Imports will immediately load the imported document, resolve subresources and execute JavaScript, if any.

Petar Nikov
  • 1,159
  • 10
  • 17
  • This is an experimental solution! Chrome supports this in version 31, and Firefox will not ship support for this. http://caniuse.com/#search=import – Adam Azad Dec 28 '15 at 08:49
  • Than take a look at this accepted answer : http://stackoverflow.com/a/9003363/3161980 – Petar Nikov Dec 28 '15 at 09:00
0

Instead of making on html file that include all js and css files. the more practical Solution is to merge all your javascript/CSS files into one file. then include that file into your html. Their are tools available that can help you to achieve this automatically e.g Gulp, YEOMAN etc.