Put HTML head in another file

2019-04-09 15:23发布

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

4条回答
Deceive 欺骗
2楼-- · 2019-04-09 15:37

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

查看更多
Evening l夕情丶
3楼-- · 2019-04-09 15:42

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.

查看更多
趁早两清
4楼-- · 2019-04-09 15:43

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
查看更多
5楼-- · 2019-04-09 15:55

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.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>
查看更多
登录 后发表回答