Place your HTML header in another file

Is it possible to include everything meta tag

, link rel

and script src

in one file, and then require that file?

The reason I am asking is because I have 10-15 lines that I copy to each file and believe there may be another way.

I put everything in one file and tried the Jquery load function but to no avail.

thank

+3


source to share


4 answers


Create a head.html file , for example:

<link rel="stylesheet" href="css/bootstrap.min.css">
<!-- Meta -->
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<!-- JS -->
<script type="text/javascript" src="js/lib/jquery-1.11.1.min.js" ></script>
<script type="text/javascript" src="js/lib/angular.min.js"></script>
<title>Your application</title>

      



Now include header.html in your HTML pages like this:

<head>
   <script type="text/javascript" src="js/lib/jquery-1.11.1.min.js" ></script>
   <script> 
       $(function(){ $("head").load("header.html") });
   </script>
</head>

      

+5


source


If you want to use jquery, you can use the get function and then add the values ​​of your files to the tag.

It will look something like this:



  //don't forget to include the file extention in the file path
  var include = "pathOfYourIncludeFile";

    $.get(include, function(data){//begin jquery get function

 //append the data returned from your file  and append it to the head
 $("head").append(data);




 });//end jquery get function

      

+2


source


With only js and html you have 2 ways:

1) create a js script and load each page (not so much different from the current one, but copy much less lines)

2) just execute 1 page and update some of the content (or the whole body) with an Ajax call.

What you're looking for is server side preprocessing that "bundles" the parts, so it's very easy to do it with php (include), java / jsp, .net, etc.

+1


source


Maybe you can use php echo for every page. for example: <?php include 'path/to/your/file'; ?>

0


source







All Articles