Put HTML head in another file

10,259

Solution 1

Create a head.html file like this:

<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.min.js" ></script>
<script type="text/javascript" src="js/lib/angular.min.js"></script>
<title>Your application</title>

Now include head.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("head.html") });
   </script>
</head>

Solution 2

If you are wanting to use jquery you can use the get function and then append your files values to the tag.

It would look something like the following:

  //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

Solution 3

With only js and html you have 2 ways:

1) create a js script and load it every page (not so much different from now but copy much less rows)

2) just do 1 page and refresh part of the content (or whole body) with an Ajax call.

What are you looking for it's a server side pre-processing that "merge" parts so it's very simple to do with php (include), java/jsp, .net and so on.

Share:
10,259
NorCalKnockOut
Author by

NorCalKnockOut

Updated on August 07, 2022

Comments

  • NorCalKnockOut
    NorCalKnockOut over 1 year

    Is there a way to include all the meta tag, link rel and script src includes in one file, and then require that file?

    The reason I ask is because I have like 10-15 lines that i am copying into every file and figured there might be another way.

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

    Thanks