Hey guys, today 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.
Final Output
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
Create HTML
Step 1:- Create HTML : include.html
include.html [HTML File]
![]() |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>How to add header and footer in JavaScript</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="common.js"></script>
</head>
<body>
<!--Start Header-->
<script type="text/javascript">
header ();
</script>
<!--End Header-->
<!--Start Middle-->
<div id="mdlContainer">
<div class="mdlLftMain">
<script type="text/javascript">
leftMenu();
</script>
</div>
<div class="mdlRtMain">
<h1 style="font-size:16px; color:#0085e3;">Home</h1>
<p>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.</p>
<script type="text/javascript">
showTxt();
</script>
</div>
<div class="clr"></div>
</div>
<div>fsa</div>
<!--End Middle-->
<!--Start Footer-->
<script type="text/javascript">
footerContainer();
</script>
<!--End Footer-->
</body>
</html>
Create HTML
Step 2:- Create HTML : product.html
product.html [HTML File]
![]() |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>How to add header and footer in JavaScript</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="common.js"></script>
</head>
<body>
<!--Start Header-->
<script type="text/javascript">
header ();
</script>
<!--End Header-->
<!--Start Middle-->
<div id="mdlContainer">
<div class="mdlLftMain">
<script type="text/javascript">
leftMenu();
</script>
</div>
<div class="mdlRtMain">
<h1 style="font-size:16px; color:#0085e3;">Product</h1>
<p>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.</p>
</div>
<div class="clr"></div>
</div>
<div>fsa</div>
<!--End Middle-->
<!--Start Footer-->
<script type="text/javascript">
footerContainer();
</script>
<!--End Footer-->
</body>
</html>
Create HTML
Step 3:- Create HTML : about.html
about.html [HTML File]
![]() |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>How to add header and footer in JavaScript</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="common.js"></script>
</head>
<body>
<!--Start Header-->
<script type="text/javascript">
header ();
</script>
<!--End Header-->
<!--Start Middle-->
<div id="mdlContainer">
<div class="mdlLftMain">
<script type="text/javascript">
leftMenu();
</script>
</div>
<div class="mdlRtMain">
<h1 style="font-size:16px; color:#0085e3;">About</h1>
<p>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.</p>
</div>
<div class="clr"></div>
</div>
<div>fsa</div>
<!--End Middle-->
<!--Start Footer-->
<script type="text/javascript">
footerContainer();
</script>
<!--End Footer-->
</body>
</html>
Create HTML
Step 4:- Create HTML : contact.html
contact.html [HTML File]
![]() |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>How to add header and footer in JavaScript</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="common.js"></script>
</head>
<body>
<!--Start Header-->
<script type="text/javascript">
header ();
</script>
<!--End Header-->
<!--Start Middle-->
<div id="mdlContainer">
<div class="mdlLftMain">
<script type="text/javascript">
leftMenu();
</script>
</div>
<div class="mdlRtMain">
<h1 style="font-size:16px; color:#0085e3;">Contact</h1>
<p>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.</p>
</div>
<div class="clr"></div>
</div>
<div>fsa</div>
<!--End Middle-->
<!--Start Footer-->
<script type="text/javascript">
footerContainer();
</script>
<!--End Footer-->
</body>
</html>
Create HTML
Step 5:- Create HTML : product_1.html
product_1.html [HTML File]
![]() |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>How to add header and footer in JavaScript</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="common.js"></script>
</head>
<body>
<!--Start Header-->
<script type="text/javascript">
header ();
</script>
<!--End Header-->
<!--Start Middle-->
<div id="mdlContainer">
<div class="mdlLftMain">
<script type="text/javascript">
leftMenu();
</script>
</div>
<div class="mdlRtMain">
<h1 style="font-size:16px; color:#0085e3;">Product1</h1>
<p>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.</p>
</div>
<div class="clr"></div>
</div>
<div> ;</div>
<!--End Middle-->
<!--Start Footer-->
<script type="text/javascript">
footerContainer();
</script>
<!--End Footer-->
</body>
</html>
Create JavaScript
Step 6:- Create JavaScript : common.js
common.js [JavaScript Controller]
![]() |
function header () {
with(document) {
write("<div id='headerMain'>");
write("<div class='logo'>Logo")
write("</div>");
write("<div class='menuMain'>");
write("<ul class='menu'>");
write("<li><a href='include.html' id='rgtlink1' name='rgtlink1'>Home</a>");
write("</li>")
write("<li><a href='product.html' id='rgtlink2' name='rgtlink2' onmouseover='show(\"t1\",\"rgtlink2\")' onmouseout='hide(\"t1\",\"rgtlink2\")'>Products</a>");
write("<div style='display:none;' id='t1' onmouseover='show(\"t1\",\"rgtlink2\")' onmouseout='hide(\"t1\",\"rgtlink2\")' class='sublnkBox'>");
write("<ul>");
write("<li><a href='product_1.html' id='rgtlink01' name='rgtlink01'>Product1</a></li>");
write("<li><a href='product_2.html' id='a02' name='a02'>Product2</a></li>");
write("<li><a href='#'>Product3</a></li>");
write("<li><a href='#'>Product4</a></li>");
write("<li><a href='#'>Product5</a></li>");
write("</ul>");
write("</div>");
write("</li>")
write("<li><a href='about.html' id='a1' name='a1'>About Us</a>");
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("<div id='footerMain'>");
write("<div class='footerLnk'><a href='#'>Home</a> | <a href='#'>About Us</a> | <a href='#'>Contact Us</a></div>");
write("</div>");
}
}
function leftMenu(){
with(document){
write("<ul class='leftMenu'>");
write("<li><a href='#'>Listing1</a></li>");
write("<li><a href='#'>Listing2</a></li>");
write("<li><a href='#'>Listing3</a></li>");
write("<li><a href='#'>Listing4</a></li>");
write("<li><a href='#'>Listing5</a></li>");
write("<li><a href='#'>Listing6</a></li>");
write("<li><a href='#'>Listing7</a></li>");
write("<li><a href='#'>Listing8</a></li>");
write("</ul>");
}
}
function showTxt(){
with(document){
write("<div><a href='#' onmouseover='showDiv(\"d1\")' onmouseout='hideDiv(\"d1\")'>Click here</a></div>");
write("<div style='display:none;' id='d1'>This is default Description. This is default Description.This is default Description.</div>");
}
}
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
stylesheet.css [Style 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;}
Further post that would you like to learn in Salesforce
How to include header file in JavaScript?
To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the
tags in your HTML document.
Should JavaScript include header or footer?
Javascript should be placed in footer of html document wherever possible. For libraries like jQuery we don't often have a choice as there may be some other javascript code which depends on that. But we should design site in such a way that maximum javascript code is placed in the end.
How to create new header in JavaScript?
The headerCreate() method creates a header element using the createElement() method upon document object and assigns it to variable h. It then calls the appendChild() method on the document body to append the header as a child to document body.
Related Topics | You May Also Like
Our Free Courses โ
๐ Get Free Course โ
![]() ๐ Salesforce Administrators ๐ Salesforce Lightning Flow Builder ๐ Salesforce Record Trigger Flow Builder |
๐ Get Free Course โ
![]() ๐ Aura Lightning Framework ๐ Lightning Web Component (LWC) ๐ Rest APIs Integration |