In this post we are going to learn about How to include header and footer in all pages uses of JavaScript function in HTML file.
<title>How to add header and footer in JavaScript</title>
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript">
header ();
</script>
<script type="text/javascript">
leftMenu();
</script>
Home
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
<script type="text/javascript">
showTxt();
</script>
</div>
<div class="clr"></div>
fsa
<script type="text/javascript">
footerContainer();
</script>
`
Create HTML
Step 2:- Create HTML : product.html
`
<title>How to add header and footer in JavaScript</title>
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript">
header ();
</script>
<script type="text/javascript">
leftMenu();
</script>
Product
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
fsa
<script type="text/javascript">
footerContainer();
</script>
`
Create HTML
Step 3:- Create HTML : about.html
`
<title>How to add header and footer in JavaScript</title>
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript">
header ();
</script>
<script type="text/javascript">
leftMenu();
</script>
About
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
fsa
<script type="text/javascript">
footerContainer();
</script>
`
Create HTML
Step 4:- Create HTML : contact.html
`
<title>How to add header and footer in JavaScript</title>
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript">
header ();
</script>
<script type="text/javascript">
leftMenu();
</script>
Contact
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
fsa
<script type="text/javascript">
footerContainer();
</script>
`
Create HTML
Step 5:- Create HTML : product_1.html
`
<title>How to add header and footer in JavaScript</title>
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript">
header ();
</script>
<script type="text/javascript">
leftMenu();
</script>
Product1
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
;
<script type="text/javascript">
footerContainer();
</script>
`
Create JavaScript
Step 6:- Create JavaScript : common.js
`function header () {
with(document) {
write("
");
write("
Logo")
write("
");
write("
");
write("
");
write("- Home");
write(" ")
write("- Products");
write("");
write(" ")
write("- About Us");
write("</li>")
write("<li><a href='contact.html' id='rgtlink3' name='rgtlink3'>Contact Us</a>");
write("</li>")
write("</ul>");
write("</div>");
write("</div>");
}
imgsearch();
}
function footerContainer(){
with(document){
write("
");
}
}
function leftMenu(){
with(document){
write("
");
}
}
function showTxt(){
with(document){
write("
");
write("This is default Description. This is default Description.This is default Description.
");
}
}
var act='';
function show(sid,cid)
{
if(document.getElementById(cid).className=='sel')
{
act=cid;
}
document.getElementById(sid).style.display='block';
document.getElementById(cid).className='sel';
}
function hide(sid,cid)
{
document.getElementById(sid).style.display='none';
document.getElementById(cid).className='';
if(act==cid)
{
document.getElementById(act).className='sel';
}
}
function showDiv(d)
{
document.getElementById(d).style.display='block';
}
function hideDiv(d)
{
document.getElementById(d).style.display='none';
}
function imgsearch() {
var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
//alert(sPage)
///////////////////about debt///////////////////////
if (sPage.toLowerCase() == 'include.html'.toLowerCase()) {
setdeafulturlwith('', 'rgtlink1', 'rgtlink1');
}
if (sPage.toLowerCase() == 'about.html'.toLowerCase()) {
setdeafulturlwith('', 'a1', 'a1');
}
//Start Sub menu//
if (sPage.toLowerCase() == 'product_1.html'.toLowerCase()) {
setdeafulturlwith('', 'rgtlink2', 'rgtlink2');
setdeafulturlwith('', 'rgtlink01', 'rgtlink01');
}
if (sPage.toLowerCase() == 'product_2.html'.toLowerCase()) {
setdeafulturlwith('', 'a1', 'a1');
setdeafulturlwith('', 'a02', 'a02');
}
//End Sub menu//
if (sPage.toLowerCase() == 'product.html'.toLowerCase()) {
setdeafulturlwith('', 'rgtlink2', 'rgtlink2');
}
if (sPage.toLowerCase() == 'contact.html'.toLowerCase()) {
setdeafulturlwith('', 'rgtlink3', 'rgtlink3');
}
}
function setdeafulturlwith(pgname, hrefid, td_id) {
document.getElementById(hrefid).removeAttribute("href");
document.getElementById(td_id).onclick = '';
document.getElementById(td_id).className = "sel";
}`
Create LWC Style CSS
Step 7:- Create Style CSS : stylesheet.css
` body{ margin:0; padding:0; font-family:Arial, Helvetica, sans-serif; color:#333333; font-size:12px;}
.over{ overflow:hidden;}
.fl{ float:left;}
.fr{ float:right;}
.clr{ clear:both;}
#headerMain{ height:100px; border-bottom:1px #ccc solid; width:800px; margin:auto;}
.logo{ font-size:18px; color:#000000; float:left; padding:30px 0 0 0;}
.menuMain{ width:500px; float:right; padding-top:75px;}
ul.menu{ margin:0; padding:0; list-style:none;}
ul.menu li{ font-size:12px; color:#000000; float:left; border-right:1px #ccc solid; position:relative;}
ul.menu li a, ul.menu li a:visited{ color:#000000; text-decoration:none; background:#eee; padding:5px 10px; display:block;}
ul.menu li a:hover, ul.menu li a.sel, ul.menu li a.sel:visited{ color:#ff0000; text-decoration:none; background:#d1d1d1;}
ul.menu li .sublnkBox{ width:200px; border:1px #ccc solid; border-top:0; border-bottom:0; background:#eee; position:absolute; top:25px; left:0;}
ul.menu li .sublnkBox ul{ margin:0; padding:0; list-style:none;}
ul.menu li .sublnkBox ul li{font-size:12px; color:#000000; float:none; border-right:0; border-bottom:1px #ccc solid;}
ul.menu li .sublnkBox ul li a, ul.menu li .sublnkBox ul li a:visited{color:#000000; text-decoration:none; background:#none; padding:5px 10px; display:block;}
ul.menu li .sublnkBox ul li a:hover{color:#ff0000; text-decoration:none; background:#d1d1d1;}
#mdlContainer{ width:800px; margin:auto; padding:20px 0 0 0; min-height:500px;}
.mdlLftMain{ width:200px; float:left; border-right:1px #ccc solid; min-height:450px;}
ul.leftMenu{ margin:0; padding:0; list-style:none;}
ul.leftMenu li{ color:#000000; font-size:12px; border-bottom:1px #ccc solid;}
ul.leftMenu li a, ul.leftMenu li a:visited{ color:#000000; text-decoration:none; background:#eee; padding:5px 10px; display:block;}
ul.leftMenu li a:hover{ color:#ff0000; text-decoration:none; background:#ccc;}
.mdlRtMain{ width:575px; float:right;}
#footerMain{ width:800px; margin:auto; border-top:1px #ccc solid; padding:10px 0 0 0;}
.footerLnk{ font-size:12px; color:#333333;}
.footerLnk a, .footerLnk a:visited{ color:#333333; text-decoration:none; margin:0 10px 0 10px;}
.footerLnk a:hover{ color:#ff0000; text-decoration:none;}`
In this post we are going to learn about How to include header and footer in all pages uses of JavaScript function in HTML file.
👉 Get source code live demo link:-
Create HTML
Step 1:- Create HTML : include.html
`
<title>How to add header and footer in JavaScript</title> <script type="text/javascript" src="common.js"></script> <script type="text/javascript"> header (); </script>Home
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Create HTML
Step 2:- Create HTML : product.html
`
<title>How to add header and footer in JavaScript</title> <script type="text/javascript" src="common.js"></script> <script type="text/javascript"> header (); </script>Product
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Create HTML
Step 3:- Create HTML : about.html
`
<title>How to add header and footer in JavaScript</title> <script type="text/javascript" src="common.js"></script> <script type="text/javascript"> header (); </script>About
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Create HTML
Step 4:- Create HTML : contact.html
`
<title>How to add header and footer in JavaScript</title> <script type="text/javascript" src="common.js"></script> <script type="text/javascript"> header (); </script>Contact
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Create HTML
Step 5:- Create HTML : product_1.html
`
<title>How to add header and footer in JavaScript</title> <script type="text/javascript" src="common.js"></script> <script type="text/javascript"> header (); </script>Product1
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Create JavaScript
Step 6:- Create JavaScript : common.js
`function header () {
with(document) {
write("
write("
write("
write("
write("
");- Home");
")- Products");
"); "); ")- About Us");
");Home | About Us | Contact Us "); ");Click here ");This is default Description. This is default Description.This is default Description. ");
write("
write("
write("
write("
write("
");- Product1
");- Product2
");- Product3
");- Product4
");- Product5
");
");write("
write("
write("
write("
write("
write("
write("
write("
write("
}
function footerContainer(){
with(document){
write("
write("
write("
}
}
function leftMenu(){
with(document){
write("
");- Listing1
");- Listing2
");- Listing3
");- Listing4
");- Listing5
");- Listing6
");- Listing7
");- Listing8
");
");write("
write("
write("
write("
write("
write("
write("
write("
write("
}
}
function showTxt(){
with(document){
write("
write("
}
}
var act='';
function show(sid,cid)
{
if(document.getElementById(cid).className=='sel')
{
act=cid;
}
document.getElementById(sid).style.display='block';
document.getElementById(cid).className='sel';
}
function hide(sid,cid)
{
document.getElementById(sid).style.display='none';
document.getElementById(cid).className='';
if(act==cid)
{
document.getElementById(act).className='sel';
}
}
function showDiv(d)
{
document.getElementById(d).style.display='block';
}
function hideDiv(d)
{
document.getElementById(d).style.display='none';
}
function imgsearch() {
var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
//alert(sPage)
///////////////////about debt///////////////////////
if (sPage.toLowerCase() == 'include.html'.toLowerCase()) {
setdeafulturlwith('', 'rgtlink1', 'rgtlink1');
}
if (sPage.toLowerCase() == 'about.html'.toLowerCase()) {
setdeafulturlwith('', 'a1', 'a1');
}
Create LWC Style CSS
Step 7:- Create Style CSS : stylesheet.css
` body{ margin:0; padding:0; font-family:Arial, Helvetica, sans-serif; color:#333333; font-size:12px;}
.over{ overflow:hidden;}
.fl{ float:left;}
.fr{ float:right;}
.clr{ clear:both;}
#headerMain{ height:100px; border-bottom:1px #ccc solid; width:800px; margin:auto;}
.logo{ font-size:18px; color:#000000; float:left; padding:30px 0 0 0;}
.menuMain{ width:500px; float:right; padding-top:75px;}
ul.menu{ margin:0; padding:0; list-style:none;}
ul.menu li{ font-size:12px; color:#000000; float:left; border-right:1px #ccc solid; position:relative;}
ul.menu li a, ul.menu li a:visited{ color:#000000; text-decoration:none; background:#eee; padding:5px 10px; display:block;}
ul.menu li a:hover, ul.menu li a.sel, ul.menu li a.sel:visited{ color:#ff0000; text-decoration:none; background:#d1d1d1;}
ul.menu li .sublnkBox{ width:200px; border:1px #ccc solid; border-top:0; border-bottom:0; background:#eee; position:absolute; top:25px; left:0;}
ul.menu li .sublnkBox ul{ margin:0; padding:0; list-style:none;}
ul.menu li .sublnkBox ul li{font-size:12px; color:#000000; float:none; border-right:0; border-bottom:1px #ccc solid;}
ul.menu li .sublnkBox ul li a, ul.menu li .sublnkBox ul li a:visited{color:#000000; text-decoration:none; background:#none; padding:5px 10px; display:block;}
ul.menu li .sublnkBox ul li a:hover{color:#ff0000; text-decoration:none; background:#d1d1d1;}
#mdlContainer{ width:800px; margin:auto; padding:20px 0 0 0; min-height:500px;}
.mdlLftMain{ width:200px; float:left; border-right:1px #ccc solid; min-height:450px;}
ul.leftMenu{ margin:0; padding:0; list-style:none;}
ul.leftMenu li{ color:#000000; font-size:12px; border-bottom:1px #ccc solid;}
ul.leftMenu li a, ul.leftMenu li a:visited{ color:#000000; text-decoration:none; background:#eee; padding:5px 10px; display:block;}
ul.leftMenu li a:hover{ color:#ff0000; text-decoration:none; background:#ccc;}
.mdlRtMain{ width:575px; float:right;}
#footerMain{ width:800px; margin:auto; border-top:1px #ccc solid; padding:10px 0 0 0;}
.footerLnk{ font-size:12px; color:#333333;}
.footerLnk a, .footerLnk a:visited{ color:#333333; text-decoration:none; margin:0 10px 0 10px;}
.footerLnk a:hover{ color:#ff0000; text-decoration:none;}`