0

Is there a way to embed the HTML from a file into another file? It would be useful for making headers or footers, not needing to paste the same code in every file.

6
  • 1
    Use php include for that. Commented Jan 18, 2014 at 13:06
  • What serverside language do u use? Commented Jan 18, 2014 at 13:07
  • @VikasSangle but is there a way to do it without PHP? Commented Jan 18, 2014 at 13:07
  • @kyr You can do it using ajax Commented Jan 18, 2014 at 13:08
  • 1
    Use jquery stackoverflow.com/questions/8988855/… Commented Jan 18, 2014 at 13:10

4 Answers 4

2

You can call them by using jQuery code, Insert it into your webpage head.

<head>

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>     

<script> 
 $(function(){
  $("#includedContent").load("/path/to/filename.html"); 
  });
</script> 

</head>

And You can show it anywhere in your webpage by inserting

 <div id="includedContent"></div>
Sign up to request clarification or add additional context in comments.

1 Comment

See updated code to call the file in your webpage!
1

This works with php:

<?php 
    include("header.html");
?>
<p>Here you can write your text</p>
<?php
    include("footer.html");
?>

Thats the simplest working

Comments

1

If you don't want to use php, you can combine grunt with a twig generator, you will have the power of Twig (example: includes, layout inheritence, etc.) in your html pages.

Then with grunt build you will generate a dist folder with all the finals html pages.

Example of generator: https://github.com/polem/generator-gruig

1 Comment

This is the way to go, I use Jade though.
0

You may want to consider using Master pages as in asp.net or else use iframes where the header, footer and iframe reside on the parent and the content is loaded into the child iframe.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.