user3304642
user3304642

Reputation: 191

Is it possible to create headerfile of javascript and css scripts ?

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>

Upvotes: 0

Views: 347

Answers (5)

Suman Bogati
Suman Bogati

Reputation: 6349

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); 

Upvotes: 3

Albzi
Albzi

Reputation: 15619

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

Upvotes: 1

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

Upvotes: 0

Mattos
Mattos

Reputation: 447

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.

Upvotes: 0

Johan
Johan

Reputation: 1026

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?

Upvotes: 0

Related Questions