1

I have two files, lets say a.html and b.html, both in the same folder. 'a' contains information that i want to be seen in 'b', when it is opened. How can I do that using only HTML?

5
  • 1
    Unless you use a frame/iframe, you cannot have one html file "include" another one. html isn't a programing language that can do such a thing. You'd need server-side tools (e.g. SSI, php, etc...) to directly load one file into another. Commented May 8, 2015 at 21:40
  • If you want to link to b.html, you use <a href="b.html">link to b</a>. If you want to "embed" b.html into the body of a.html you don't do that without javascript. Commented May 8, 2015 at 21:40
  • @PaulProgrammer can you tell me how it can be done with javascript. I just started writing html and i still don't know many things. Commented May 8, 2015 at 21:48
  • Switching the files to PHP would allow you to embed one in the other if that is available to you... Commented May 8, 2015 at 22:02
  • @DrCord to be specific using most server side language would allow to you manipulate the HTML document more fluidly Commented May 9, 2015 at 7:19

3 Answers 3

1

Currently you can't just include 2 HTML files together using only HTML

Sign up to request clarification or add additional context in comments.

1 Comment

also can you elaborate on what exactly you want included in the other HTML file so people can help you?
1

You can do that by using HTML Import

In your index file, you include an import to your page by simply adding

<link href="import.html" rel="import" />

To use the content of an import use the .import property.

var content = document.querySelector('link[rel="import"]').import;

To make use of a certain element.

var elm = content.querySelector('.your-class-to-use'); 

But take note, it's not being supported on all browsers check here.

1 Comment

this is technically an HTML5 feature but requires JavaScript to use the content, so this isn't an HTML only method of importing files...
0

Per comment on question, the way to embed a second HTML document into the first using javascript looks something like this:

<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
  $(function() {
    $.get("b.html", function( data ) {
      $("#target").html(data);
    });
  });
</script>
</head>
<body>
  <h1>this is a title</h1>
  <div id="target"><!-- dynamic content will be put here --></div>
  <p>
    You can put any other html you like in the rest of the document
  </p>
</body>
</html>

I used jquery here instead of raw javascript, because it abstracts differences in browser behavior, wraps potential errors and is therefore easier to get started with.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.