Embedding HTML files in other HTML pages - html

I am new to HTML. I have an html page named "main.html" and i want to include another html page called "menu.html" in it. My main.html page doesn't include frames and it is designed using div tags. My site is hosted on linux based server The site I have to redesign is Java questions.

You want to look at Server Side Includes (SSI). This tutorial by Apache should get you up and running if that site is running on Apache.
There are plenty of Server Side ways of doing this, but all except SSI require the use of a language other than HTML.
If you're using IIS, you can check out Microsoft's writeup on Server Side Includes.

Check out server side includes.

You should use server-side includes.
i.e. in jsp you can use: <c:import url="/include/navigation.jsp" />, in php <?php include("/sidebar.php");?> and so on.
This is the good way to do what you need: include a navigation menu, or other parts common to all pages, without rewriting it in each page.
You can also do the same in other ways (with some javascript i.e.) but I doubt that you want to build a site called Java Questions without any server side language.

I think that PHP is the easiest way to do this. Most of the time you can just change your main.html file to main.php then add this php code where you want the menu bar:
<?php include('menu.html'); ?>
And that's it! You have to make sure that php is installed on your sever. Also this will ONLY work on a server. So if you are testing on your computer and using something like dreamweaver (or even a browser), you won't see anything until it's online.

You can make an ajax call in javascript if you want to avoid using the server.
In JQuery you would do:
<div id="putStuffHere">
</div>
<script>
$('#putStuffHere').load('myStuff.html');
</script>

Related

Reuse html in different pages

This is my website, https://unrealcousinzzz.com/.
I made it in HTML, CSS, and JavaScript. I cannot use PHP or anything like that right now because I am hosting it in AWS S3, because lightsail is too expensive for me. On each page in my website, I use the same HTML code. For example, every page has code for my navbar, and code google AdSense.
If I want to change these, I would have to do it on every page right now. Is there a way to make a file that other HTML pages will read, and use that where it is placed in that page?
HTML itself doesn't have any sort of templating, but there are alternatives.
Use something on your dev machine to compile your HTML for you. I use a Node.js script and a JavaScript template engine for this. Basically, I write out my templates, run the script which generates the HTML, and then I upload it to the S3 bucket.
It doesn't have to be done with Node. You could use PHP with this same method. Or, a Bash script for that matter.
Ok, so for me google tag manager is good. I can add custom html, and it goes onto all of my pages.

Import HTML without using PHP?

Is there a way to import a block of HTML into a file without the use of something like PHP or Javascript? For my personal website, any time I make a small change to the content of my navbar, I have to go to all of my other pages and make that same change.
Do an internet search for "Server Side Includes for HTML"
Here's a good start:
http://en.wikipedia.org/wiki/Server_Side_Includes
Example:
<!--#include virtual="header.html" -->
No feature like the one you described is in the HTML spec, but if you don't have access to server configurations and the like, you can write your code in a language like HAML and compile it, which would accomplish what you described without any server-side work.

call single css menu in multiple html pages

I'm in the process of creating a website.
I have a menu bar which is common for all the pages. I want to call the HTML page which contains the menu bar from any page within my site.
Is there any syntax to include HTML files so that I can solve my issue.
If so how to do it?
You need to use php extension for you files instead of html and call your header with
<?php include_once('header.php'); ?>
where you want header to be displayed
If you are working on local machine install xampp or you will not be able to see your website because php is server side scripting language
** Answer from other questions **
You are describing server side includes.
Server Side Includes are useful for including a common piece of code throughout a site, such as a page header, a page footer and a navigation menu.
Example of usage:
<!--#include virtual="../quote.txt" -->
This will add the text of quote.txt to the page - if you add it to multiple pages, you can make your change in this one file and it will show on all pages in was included in.
Different web servers have different support and additional features for these, so you need to check the documentation for yours.
Many websites that need dynamic features (like fetching data from a database) will use some kind of server side scripting language for this kind of functionality - examples include PHP, Perl, ASP.NET, JSP and many more.

How to apply same content to multiple html pages

I'm new to html and was wondering if there is a way to apply the same content to many html files at once.
For example, if I have many pages, but all those pages have an identical navigation side panel that contains links to all the other pages. Is there any way to change the contents of this side panel without changing it for each individual page?
i.e. is there a feature that allows me to make this navigation panel in a separate file, then tell all my pages to include this navigation file?
I know a css file can control the format of many html pages - is there an analogy to this that can control the content of many html pages?
You can use PHP to do that. Write the HTML code in PHP file, then add include statement in your HTML. This saves you from having to write same code again and again specially for navigation, etc.
PHP manual explains it.
Hope it helps.
You can write the common content in javascript file and include it in your html pages using script tag:
<script src="YOUR_FILE.js"></script>
You can use an online HTML to Javascript converter like this one to generate you javascript code.
Server-side includes or server-side programming languages (like PHP, for example), are often used to do that. All pages just include a shared common file, which contains the common content.
<?php
include(file with extension);
?>
You'd have to change your file extension that runs this code to DOT php

Avoid repeating headers & footers

I am building a website with at least 7 pages and am looking for a way to avoid having to repeat all headers & footers etc. on every single html document.
I know there are frames and iframes but some browsers don't support these and I want to do something more efficient and flexible for updates. Any ideas/suggestions?
Thanks for the information guys! I'll be sure to try them out.
One more thing is that I have a login form within my header. Will these methods affect it?
Regards
Yes, server side includes are the way to go, just a few clarifications:
1) You can do it as described by Sotiris if you choose to use PHP - which is very common choice, and btw.virtually all hosts support php.
2) Although the file will have extension .php, you can put your pure html code in it.
3) To be able to test and see this on your computer before uploading it, you have to install server on it. At this point I think it's simplest and fastest for you to go for some ready made solution - these are downloadable free:
http://www.wampserver.com/en/ (for Windows)
http://www.mamp.info/en/index.html (for Mac)
http://tuxtweaks.com/2010/04/installing-lamp-on-ubuntu-10-04-lucid-lynx/ (for Linux)
you can create a php file that will include all required code (for example footer.php). Then you can link it adding in every page in the proper place the following code (if you want to add the footer for example):
<?php include "footer.php"; ?>
If it is static HTML pages that you are builing, you probably want to think about Server Side Includes
http://en.wikipedia.org/wiki/Server_Side_Includes