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>
8
  • 2
    use php's include function Commented Mar 12, 2014 at 13:59
  • 2
    java and javascript is something completely different. Commented Mar 12, 2014 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. Commented Mar 12, 2014 at 14:01
  • Did you mean Javascript? Commented Mar 12, 2014 at 14:03
  • 1
    You already answered your own question. You basically make a file containing the code from your first block (containing all those <script> tags, as well as <style type="text/css"> tags [do NOT use <link rel="..."> tags please]. And then inside your PHP file you do a <html><head><?php include('that_file_file');?></head><body>...</body></hmtl> Commented Mar 12, 2014 at 14:05

5 Answers 5

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); 
Sign up to request clarification or add additional context in comments.

3 Comments

if in case I am not using php then ? only html
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.
It's meant to be example.php, not exmaple.php. I've fixed it for the confusion (on @JohannesN's reply).
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');

5 Comments

Should I include css in same all_java_css.html ?
All css and javascript.
confused because in above comment tularis told that don't include link rel="">
I'm not web savvy enough to know why that is. Maybe ask him? @Peter
Thanbks, glad I could help @Peter
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?

Comments

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.

Comments

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';
    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.