0

I have no much idea about web programming, I am looking for some way to create header file for javascript and css whether its possible ?

here is scenario, assume I have 100s of javascript and css if I have to create html or php script everytime I have to put these line

<script type="text/javascript" src="../src/jquery.1.js"></script>
<script type="text/javascript" src="../src/jquery.2.js"></script>
...
...
<script type="text/javascript" src="../src/jquery.100.js"></script>

<link rel="stylesheet" type="text/css" href="../jquery.1.css" />
<link rel="stylesheet" type="text/css" href="../jquery.2.css" />
...
...
 <link rel="stylesheet" type="text/css" href="../jquery.100.css" />

I want to create only one file which contains all javascript and css, and their path and then want to include it in html like this, I expect something kind of C

   include 'all_java_css'
   <html>
         <body>
           <p> all included</p>
        </body>
  </html>
Daan
  • 12,099
  • 6
  • 34
  • 51
user3304642
  • 181
  • 1
  • 2
  • 16
  • 2
    use php's include function – epoch Mar 12 '14 at 13:59
  • 2
    java and javascript is something completely different. – Daan Mar 12 '14 at 13:59
  • 2
    If you have *hundreds* of separate files, you should be working on a way to combine and compress them server-side. – Pointy Mar 12 '14 at 14:01
  • Did you mean Javascript? – Mattos Mar 12 '14 at 14:03
  • somebody give me demo, I want to create 1 file which contains all javascript and css to be included – user3304642 Mar 12 '14 at 14:04
  • 1
    You already answered your own question. You basically make a file containing the code from your first block (containing all those ` – Tularis Mar 12 '14 at 14:05
  • @Tularis I think he Peter wants to have this done automatically – yunzen Mar 12 '14 at 14:18
  • Guys I just want to include only file for html, most of the cases js and css I use are common so, I can't search everytime, hope its clear – user3304642 Mar 12 '14 at 14:20

5 Answers5

3

Use include() php function for this purpose, what you will have to do is.

Make a php file and put the scripts file into this file, and include that php file in your file where you need to include js files,

Suppose you made a php file example.php with js files.

example.php

<script type="text/javascript" src="../src/jquery.1.js"></script>
<script type="text/javascript" src="../src/jquery.2.js"></script>

home.php

//with this line you are including all above js files into home.php
inlcude(example.php); 
Albzi
  • 15,431
  • 6
  • 46
  • 63
Suman Bogati
  • 6,289
  • 1
  • 23
  • 34
  • if in case I am not using php then ? only html – user3304642 Mar 12 '14 at 14:07
  • `inlcude(exmaple.php);` is PHP. But in this case you will still have to include your 100 files seperatly just it looks better because it is done in a seperate file. – almo Mar 12 '14 at 14:13
  • 1
    It's meant to be `example.php`, not `exmaple.php`. I've fixed it for the confusion (on @JohannesN's reply). – Albzi Mar 12 '14 at 14:29
1

First of all, if you can try and put as many of them files into one, that would be better. Instead of calling 100 of each, you could only call a few, making run times faster.

Also, add all this scripts to a .html or .php file for example:

<script type="text/javascript" src="../src/jquery.1.js"></script>
<script type="text/javascript" src="../src/jquery.2.js"></script>

and save it as header.html.

Then in your main file, you can do this:

<html>
    <head>
        <?php include('header.html'); ?>
    </head>
    <body>
        ...   
    </body>
</html> 

You need to make sure that your main file is 1) a .php extension and 2) can actually run PHP.

Second of all, you need to make sure header.html is in the same directory as index.php or whatever the php file is.

If not, you can always use

<?php include('inc/header/all_java_css.html');?>

If you do not have PHP, you can do this: In the head:

<head>
    <meta name="add">
</head>

Then using jQuery:

$('meta[name="add"').load('all_java_css.html');
Albzi
  • 15,431
  • 6
  • 46
  • 63
0

CSS files can be imported from a single CSS file like this:

@import "newstyles1.css";
@import "newstyles2.css";
@import "newstyles3.css";

JS is a bit more tricky, but maybe this can help? How to include js file in another js file?

Community
  • 1
  • 1
Johan
  • 1,016
  • 7
  • 13
0

You could compile both .js and .css files...

Javascript - Closure tools

https://developers.google.com/closure/compiler/

CSS - LESS.org

http://lesscss.org/

By compiling both types you could import 2 files do your page.

Mattos
  • 447
  • 2
  • 8
0

i think this is better way for you, because if you put more js file in header then make some error, so you will put js file in footer

header.php

<html>
<head>
<link rel="stylesheet" type="text/css" href="../jquery.1.css" />
<link rel="stylesheet" type="text/css" href="../jquery.2.css" />
...
...
 <link rel="stylesheet" type="text/css" href="../jquery.100.css" />
</head>
<body>

footer.php

<script type="text/javascript" src="../src/jquery.1.js"></script>
<script type="text/javascript" src="../src/jquery.2.js"></script>
...
...
<script type="text/javascript" src="../src/jquery.100.js"></script>
</body>
</html>

main.php

   <?php
    include 'header.php';
    ?>
// do code for content
<?php
    include 'fotter.php';
    ?>