HTML - constant headers/auto updating headers? - html

Is it possible in HTML to create a constant header? e.g. a header that you only write in one file and goes out to all the sub webpages on the website instead of having to update like 200 webpages if you want to change the header?

Not with purely html no,
However with a little php you could.
Create a Header.php and put your header code in there.
each of your .html files becomes a .php
so you can then do
<?php include('Header.php'); ?>
Now any time you change Header.php every page gets the change.
Note: If your running this locally you will need a local "server" to run php, I personally use WampServer

Related

HTML headers and footers: edit multiple files from one place

I am currently coding up my own website (basically a personal blog) w/ HTML5 and CSS3. However, the growing number of .html files (blog posts and other stuff) quickly raised a problem: say if I want to change something in my page header / footer (which I want to keep the same across the entire site), I would have to edit every .html file to get this done. Is there anyway that this process can be simplified to a one-time action? Mostly, I write html files in Brackets. Thanks in advance!
Why not save the files as .php, create another file for the header and footer, and include the header and footer ( <?php include 'header.php'; ?> ) files from each of the main files?

Dynamic HTML Construction

Every time I decide to change something in my header/navigationbar/footer/etc. I need to apply those changes to 16 other html files so my changes are consistent across my entire website.
My question is: is there a way i can make my website's template be automatically applied to every page?
An example of any page on my website and what i have in mind would look like this:
<html>
<head>injected code</head>
<body>
<header>injected code<header>
<section>NOT INJECTED CODE</section>
<footer>injected code</footer>
</body>
</html>
I know repeating code like this is bad practice, so how do i reuse (localize) code for these areas of my html since they will always be the same?
I am not really interested in content management systems.
What i do with the elements that are the same on every page, like the header, nav, footer is create those elements in a file apart and then include via php. Then if you have to change one thing in the header you only have to do it one time.
Your example will be like:
<body>
<header><?php include_once('header.html'); ?><header>
<section>NOT INJECTED CODE</section>
<footer><?php include_once('footer.html'); ?></footer>
</body>
Hope it helps.
You can use php to solve this. Name your file "index.php" (or anything else with .php as the extension)
<html>
<?php include('header.php'); ?>
<body>
<header>injected code<header>
<section>NOT INJECTED CODE</section>
<footer>injected code</footer>
</body>
<?php include('footer.php'); ?>
Then make header.php and footer.php in the same folder. These common files can then be included in all your pages.
Tip: You will need to run these on a local server. See xampp
The best way that I can think of would be to use a server side language, such as php or asp , to generate the html.
You can use <iframe> tag to include external file however I have not properly tried this and am not sure of the security or results.
You can also use javascript/jquery to write to the document, however using scripts just to write would not be best practice, a better use for client-side language would be to use ajax to load external files however some of the header may need to be defined before the ajax is complete although I haven't tested it.
In short I would recommend using a server side language probably php as it is easy to learn, free to use and you can install it on your local machine. If you already have a server running you can see what languages are already installed as most languages can include external files.

Default HTML Page?

Alright, I don't know how exactly I'm going to phrase this, so bear with me here. Is there a way to set a default HTML page? Like is there a line of code that I can use on new html files to load a local .html file for almost a template?
Use any sever-side programming language to include header and footer parts of your default webpage.
Example on PHP:
<?php
echo file_get_contents("header.html")
?>
Your page contents
<?php
echo file_get_contents("footer.html")
?>
You can set default html page in your webserver. But that's just if someone hits http://server/ with no page name it tells what page to use like http://server/index.html versus http://server/default.apx, etc. Has nothing to do with templates.
If you want to be able to include html files inside other html files, you probably need a serverside language like ASP, PHP, JSP. HTML itself doesn't have that capability, although some webservers might offer a custom tag that is translated on server-side for includes.
The default web page on a server must have the name index.html, index.htm or sometimes default.htm. It is possible to change the server to accept other file names as default files, but those are the most commonly used ones. What is the index.html page?
If you're asking how to make your web browser open a default page when you open it, this is usually called the "Home Page" and any file or page that you can visit in your browser you can assign as the home page.
If you're trying to create a template for a web page, there are many ways to do it. The PHP example listed above is one way. You can also do it with JavaScript. Write your HTML in document.write() calls inside a file named navigation.js and then place this script where you want that code displayed:
<script type="text/javascript" src="navigation.js"> </script>
However, this is not unobtrusive and could cause problems. Another way to do it is with Server Side Includes. Write a file named navigation.ssi and then add the following SSI line to your HTML where you want the included file to display:
<!--#include virtual="/ssi/navigation.ssi" -->
You can also do it with ASP and other methods. I explain in more detail in my article How Do I Include One HTML File in Another?

"Universal" links in HTML? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Include one HTML file in another HTML file
On each of the pages on my website I have a footer that contains about 12 links. Every time I want to change a link, I have to do it on every single page. Is it possible to make it so I can somehow change the link in one place and it changes on every page? Thanks.
This exact problem is the beauty of serverside scripting, where you can define a header file that each page pulls from when it is retrieved, allowing you to change a single file and have the changes propagate your entire website.
This is what it could look like in PHP:
header.php - Link
all_other_pages.php - <?php include header.php; ?> ...
the better approach is use the mvc architecture for that . and you can do it by creating the separate php file and include them
like for header header.php for wrapper wrapper.php and for footer footer.php and to show a pagge just include or require them
like index.php
<?php
require"header.php"; // for the header of the page
require"wrapper.php";
require"footer.php"; // for the footer of the page
you can also make function of header() and footer() so you can also include many more function in a file and it reduce the amount of file also you only need to include the one file with function
index.php
<?php
require"function.php";
header(); // for the header of the page
require"wrapper.php";
footer(); // for the footer of the page
in above approach the code of header and futtor will be universal for all website what you need to change is the chande the code in wrapper.php
You don't need to do server side programming to fix this (though you can do), you can do it perfectly fine in the browser with a little javascript.
For example:
at the bottom of each page:
<div class="page-footer"/>
<script src="footer.js"></script>
in footer.js something along the lines:
$(".page-footer").append("<ul><li><a href='about.html'>About</a></li></ul>");
If you can use a server side include or other server side programming then yes, otherwise it isn't posible with straight HTML. You could include the footer in an IFRAME such that each page loads the same footer page, but this is far from an ideal solution.

How can I make a global header rather than pasting it in each page?

Every time I update my header, I go in and update each page individually. I'm pretty sure there is a way to keep the header in one file and just call it within each page.
This way I update one file and it's updated on each page automatically. I'm new in all this, so please keep it simple to understand :)
You don't say which server you are using to host the files - but most of them have the ability to do server-side includes. This would let you put your header into a separate file, and 'include' it in each html file. When the HTML file is requested, the server would replace the include statement with the contents of the header file.
Check your server documentation for more details
If your web host supports php, this is very very easy to do..
First create your header.php file and insert all the text, buttons and so forth in the header.php file.
Second create a footer.php file and insert all the text, buttons and so forth in the footer.php file.
Then create a index.php and call the include files like this.
<div><?php include 'header.php'; ?></div>
<div>Sone Content</div>
<div><?php include 'footer.php'; ?></div>
Then for the other pages follow the same guidlines. In order for this to work all of your files need to have a .php extension.
With seamless attribute your iframe tag would seems like part of your document:
<iframe src="header.htm" seamless="seamless"></iframe>
or
<iframe src="header.htm" seamless></iframe>
Just be careful about the browser support!