How to include header and footer in all pages uses of JavaScript function in HTML file | How to include header and footer in html file without php | How to load header and footer using Javascript and HTML

784 views

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

 how to add header and footer in javaScript -- w3web.net

 

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]

  1.    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>How to add header and footer in JavaScript</title>
  6. <link href="stylesheet.css" rel="stylesheet" type="text/css" />
  7. <script type="text/javascript" src="common.js"></script>
  8.  
  9. </head>
  10.  
  11. <body>
  12. <!--Start Header-->
  13. <script type="text/javascript">
  14. 	header ();
  15. </script>
  16. <!--End Header-->
  17. <!--Start Middle-->
  18. <div id="mdlContainer">
  19. 	<div class="mdlLftMain">
  20. 		<script type="text/javascript">
  21. 			leftMenu();
  22. 		</script>
  23. 	</div>
  24. 	<div class="mdlRtMain">
  25. 	    <h1 style="font-size:16px; color:#0085e3;">Home</h1>
  26. 		<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>
  27.  
  28. 		<script type="text/javascript">
  29. 			showTxt();
  30. 		</script>
  31.  
  32.  
  33.  
  34. 	</div>
  35. 	<div class="clr"></div>
  36. </div>
  37. <div>fsa</div>
  38. <!--End Middle-->
  39. <!--Start Footer-->
  40. 	<script type="text/javascript">
  41. 		footerContainer();
  42. 	</script>
  43. <!--End Footer-->
  44.  
  45. </body>
  46. </html>

 

Create HTML

Step 2:- Create HTML : product.html

product.html [HTML File]

  1.    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>How to add header and footer in JavaScript</title>
  6. <link href="stylesheet.css" rel="stylesheet" type="text/css" />
  7. <script type="text/javascript" src="common.js"></script>
  8. </head>
  9.  
  10. <body>
  11. <!--Start Header-->
  12. <script type="text/javascript">
  13. 	header ();
  14. </script>
  15. <!--End Header-->
  16. <!--Start Middle-->
  17. <div id="mdlContainer">
  18. 	<div class="mdlLftMain">
  19. 		<script type="text/javascript">
  20. 			leftMenu();
  21. 		</script>
  22. 	</div>
  23. 	<div class="mdlRtMain">
  24. 		<h1 style="font-size:16px; color:#0085e3;">Product</h1>
  25. 		<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>
  26. 	</div>
  27. 	<div class="clr"></div>
  28. </div>
  29. <div>fsa</div>
  30. <!--End Middle-->
  31. <!--Start Footer-->
  32. 	<script type="text/javascript">
  33. 		footerContainer();
  34. 	</script>
  35. <!--End Footer-->
  36. </body>
  37. </html>

 

Create HTML

Step 3:- Create HTML : about.html

about.html [HTML File]

  1.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>How to add header and footer in JavaScript</title>
  6. <link href="stylesheet.css" rel="stylesheet" type="text/css" />
  7. <script type="text/javascript" src="common.js"></script>
  8. </head>
  9.  
  10. <body>
  11. <!--Start Header-->
  12. <script type="text/javascript">
  13. 	header ();
  14. </script>
  15. <!--End Header-->
  16. <!--Start Middle-->
  17. <div id="mdlContainer">
  18. 	<div class="mdlLftMain">
  19. 		<script type="text/javascript">
  20. 			leftMenu();
  21. 		</script>
  22. 	</div>
  23. 	<div class="mdlRtMain">
  24. 		<h1 style="font-size:16px; color:#0085e3;">About</h1>
  25. 		<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>
  26. 	</div>
  27. 	<div class="clr"></div>
  28. </div>
  29. <div>fsa</div>
  30. <!--End Middle-->
  31. <!--Start Footer-->
  32. 	<script type="text/javascript">
  33. 		footerContainer();
  34. 	</script>
  35. <!--End Footer-->
  36. </body>
  37. </html>

 

Create HTML

Step 4:- Create HTML : contact.html

 
βœͺ The Big Announcement: -
As per student's demands, I re-opened my eBook of "Salesforce Tutorial" Limited-time huge discount offer Absolutely 50% Off.
I am thinking to give you discount offer occasion of Gandhi Jayanti for a powerful "Salesforce admin course" where you can Understand from Basic Concepts to advanced label in Salesforce.
πŸ‘‰ So Don't MISS it... (Access Right Now)
πŸ‘‰ Get Huge Discount Offer 50%: - Get eBook
 

contact.html [HTML File]

  1.    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>How to add header and footer in JavaScript</title>
  6. <link href="stylesheet.css" rel="stylesheet" type="text/css" />
  7. <script type="text/javascript" src="common.js"></script>
  8. </head>
  9.  
  10. <body>
  11. <!--Start Header-->
  12. <script type="text/javascript">
  13. 	header ();
  14. </script>
  15. <!--End Header-->
  16. <!--Start Middle-->
  17. <div id="mdlContainer">
  18. 	<div class="mdlLftMain">
  19. 		<script type="text/javascript">
  20. 			leftMenu();
  21. 		</script>
  22. 	</div>
  23. 	<div class="mdlRtMain">
  24. 		<h1 style="font-size:16px; color:#0085e3;">Contact</h1>
  25. 		<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>
  26. 	</div>
  27. 	<div class="clr"></div>
  28. </div>
  29. <div>fsa</div>
  30. <!--End Middle-->
  31. <!--Start Footer-->
  32. 	<script type="text/javascript">
  33. 		footerContainer();
  34. 	</script>
  35. <!--End Footer-->
  36. </body>
  37. </html>

 

Create HTML

Step 5:- Create HTML : product_1.html

product_1.html [HTML File]

  1.    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>How to add header and footer in JavaScript</title>
  6. <link href="stylesheet.css" rel="stylesheet" type="text/css" />
  7. <script type="text/javascript" src="common.js"></script>
  8. </head>
  9.  
  10. <body>
  11. <!--Start Header-->
  12. <script type="text/javascript">
  13. 	header ();
  14. </script>
  15. <!--End Header-->
  16. <!--Start Middle-->
  17. <div id="mdlContainer">
  18. 	<div class="mdlLftMain">
  19. 		<script type="text/javascript">
  20. 			leftMenu();
  21. 		</script>
  22. 	</div>
  23. 	<div class="mdlRtMain">
  24. 		<h1 style="font-size:16px; color:#0085e3;">Product1</h1>
  25. 		<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>
  26. 	</div>
  27. 	<div class="clr"></div>
  28. </div>
  29. <div>&nbsp;;</div>
  30. <!--End Middle-->
  31. <!--Start Footer-->
  32. 	<script type="text/javascript">
  33. 		footerContainer();
  34. 	</script>
  35. <!--End Footer-->
  36. </body>
  37. </html>

 

Create JavaScript

Step 6:- Create JavaScript : common.js

common.js [JavaScript Controller]

  1.   function header () {
  2. 	with(document) {		 
  3. 		 write("<div id='headerMain'>");		 
  4. 			 write("<div class='logo'>Logo")
  5. 			 write("</div>");			 
  6. 			 write("<div class='menuMain'>");			 
  7. 				 write("<ul class='menu'>");
  8. 					 write("<li><a href='include.html' id='rgtlink1' name='rgtlink1'>Home</a>");
  9. 					 write("</li>")
  10. 					 write("<li><a href='product.html' id='rgtlink2' name='rgtlink2'  onmouseover='show(\"t1\",\"rgtlink2\")' onmouseout='hide(\"t1\",\"rgtlink2\")'>Products</a>");
  11. 					   write("<div style='display:none;' id='t1' onmouseover='show(\"t1\",\"rgtlink2\")' onmouseout='hide(\"t1\",\"rgtlink2\")' class='sublnkBox'>");
  12. 							write("<ul>");
  13. 								write("<li><a href='product_1.html' id='rgtlink01' name='rgtlink01'>Product1</a></li>");
  14. 								write("<li><a href='product_2.html' id='a02' name='a02'>Product2</a></li>");
  15. 								write("<li><a href='#'>Product3</a></li>");
  16. 								write("<li><a href='#'>Product4</a></li>");
  17. 								write("<li><a href='#'>Product5</a></li>");
  18. 							write("</ul>");
  19. 						write("</div>");
  20. 					 write("</li>")
  21. 					 write("<li><a href='about.html' id='a1' name='a1'>About Us</a>");
  22.  
  23. 					 write("</li>")
  24. 					 write("<li><a href='contact.html' id='rgtlink3' name='rgtlink3'>Contact Us</a>");
  25. 					 write("</li>")										 
  26. 				 write("</ul>");			 
  27. 			 write("</div>");		 
  28. 	 write("</div>");	 
  29. 	}
  30. 	imgsearch();
  31. }
  32.  
  33.  
  34.  
  35. function footerContainer(){
  36. 		with(document){
  37. 				write("<div id='footerMain'>");
  38. 					write("<div class='footerLnk'><a href='#'>Home</a> | <a href='#'>About Us</a> | <a href='#'>Contact Us</a></div>");					
  39. 				write("</div>");
  40. 			}
  41. 	}
  42.  
  43.  
  44.  
  45. function leftMenu(){
  46. 		with(document){
  47. 				write("<ul class='leftMenu'>");
  48. 					write("<li><a href='#'>Listing1</a></li>");
  49. 					write("<li><a href='#'>Listing2</a></li>");
  50. 					write("<li><a href='#'>Listing3</a></li>");
  51. 					write("<li><a href='#'>Listing4</a></li>");
  52. 					write("<li><a href='#'>Listing5</a></li>");
  53. 					write("<li><a href='#'>Listing6</a></li>");
  54. 					write("<li><a href='#'>Listing7</a></li>");
  55. 					write("<li><a href='#'>Listing8</a></li>");
  56. 				write("</ul>");
  57. 			}
  58. 	}
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. function showTxt(){
  66. 	with(document){
  67. 			write("<div><a href='#' onmouseover='showDiv(\"d1\")' onmouseout='hideDiv(\"d1\")'>Click here</a></div>");
  68. 			write("<div style='display:none;' id='d1'>This is default Description. This is default Description.This is default Description.</div>");
  69. 		}
  70. }
  71.  
  72.  
  73.  
  74.  
  75. var act='';
  76. function show(sid,cid)
  77. {
  78. 	if(document.getElementById(cid).className=='sel')
  79. 	{
  80. 		act=cid;
  81. 	}
  82. 	document.getElementById(sid).style.display='block';
  83. 	document.getElementById(cid).className='sel';
  84. }
  85.  
  86. function hide(sid,cid)
  87. {
  88. 	document.getElementById(sid).style.display='none';
  89. 	document.getElementById(cid).className='';
  90. 	if(act==cid)
  91. 	{
  92. 		document.getElementById(act).className='sel';
  93. 	}
  94. }
  95.  
  96.  
  97.  
  98.  
  99. function showDiv(d)
  100. {
  101. 	document.getElementById(d).style.display='block';
  102. }
  103. function hideDiv(d)
  104. {
  105. 	document.getElementById(d).style.display='none';
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112. function imgsearch() {
  113.         var sPath = window.location.pathname;
  114.         var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
  115.         //alert(sPage)
  116.         ///////////////////about debt///////////////////////		
  117.         if (sPage.toLowerCase() == 'include.html'.toLowerCase()) {
  118.             setdeafulturlwith('', 'rgtlink1', 'rgtlink1');
  119.         }
  120.         if (sPage.toLowerCase() == 'about.html'.toLowerCase()) {
  121.             setdeafulturlwith('', 'a1', 'a1');
  122.         }
  123.  
  124. 		//Start Sub menu//
  125. 		if (sPage.toLowerCase() == 'product_1.html'.toLowerCase()) {
  126.             setdeafulturlwith('', 'rgtlink2', 'rgtlink2');
  127. 			setdeafulturlwith('', 'rgtlink01', 'rgtlink01');
  128.         }
  129. 		if (sPage.toLowerCase() == 'product_2.html'.toLowerCase()) {
  130.             setdeafulturlwith('', 'a1', 'a1');
  131. 			setdeafulturlwith('', 'a02', 'a02');
  132.         }
  133.  
  134. 		//End Sub menu//
  135.  
  136.  
  137.         if (sPage.toLowerCase() == 'product.html'.toLowerCase()) {
  138.             setdeafulturlwith('', 'rgtlink2', 'rgtlink2');
  139.         }
  140. 		 if (sPage.toLowerCase() == 'contact.html'.toLowerCase()) {
  141.             setdeafulturlwith('', 'rgtlink3', 'rgtlink3');
  142.         }       
  143.     }
  144.  
  145.     function setdeafulturlwith(pgname, hrefid, td_id) {
  146.         document.getElementById(hrefid).removeAttribute("href");
  147.         document.getElementById(td_id).onclick = '';
  148.         document.getElementById(td_id).className = "sel";
  149.     }

 

Create LWC Style CSS

Step 7:- Create Style CSS : stylesheet.css

stylesheet.css [Style CSS]

  1.    body{ margin:0; padding:0; font-family:Arial, Helvetica, sans-serif; color:#333333; font-size:12px;}
  2. .over{ overflow:hidden;}
  3. .fl{ float:left;}
  4. .fr{ float:right;}
  5. .clr{ clear:both;}
  6. #headerMain{ height:100px; border-bottom:1px #ccc solid; width:800px; margin:auto;}
  7. .logo{ font-size:18px; color:#000000; float:left; padding:30px 0 0 0;}
  8. .menuMain{ width:500px; float:right; padding-top:75px;}
  9.  
  10. ul.menu{ margin:0; padding:0; list-style:none;}
  11. ul.menu li{ font-size:12px; color:#000000; float:left; border-right:1px #ccc solid; position:relative;}
  12. ul.menu li a, ul.menu li a:visited{ color:#000000; text-decoration:none; background:#eee; padding:5px 10px; display:block;}
  13. ul.menu li a:hover, ul.menu li a.sel, ul.menu li a.sel:visited{ color:#ff0000; text-decoration:none; background:#d1d1d1;}
  14.  
  15. ul.menu li .sublnkBox{ width:200px; border:1px #ccc solid; border-top:0; border-bottom:0; background:#eee; position:absolute; top:25px; left:0;}
  16. ul.menu li .sublnkBox ul{ margin:0; padding:0; list-style:none;}
  17. ul.menu li .sublnkBox ul li{font-size:12px; color:#000000; float:none; border-right:0; border-bottom:1px #ccc solid;}
  18. 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;}
  19. ul.menu li .sublnkBox ul li a:hover{color:#ff0000; text-decoration:none; background:#d1d1d1;}
  20.  
  21.  
  22. #mdlContainer{ width:800px; margin:auto; padding:20px 0 0 0; min-height:500px;}
  23. .mdlLftMain{ width:200px; float:left; border-right:1px #ccc solid; min-height:450px;}
  24. ul.leftMenu{ margin:0; padding:0; list-style:none;}
  25. ul.leftMenu li{ color:#000000; font-size:12px; border-bottom:1px #ccc solid;}
  26. ul.leftMenu li a, ul.leftMenu li a:visited{ color:#000000; text-decoration:none; background:#eee; padding:5px 10px; display:block;}
  27. ul.leftMenu li a:hover{ color:#ff0000; text-decoration:none; background:#ccc;}
  28.  
  29. .mdlRtMain{ width:575px; float:right;}
  30.  
  31. #footerMain{ width:800px; margin:auto; border-top:1px #ccc solid; padding:10px 0 0 0;}
  32. .footerLnk{ font-size:12px; color:#333333;}
  33. .footerLnk a, .footerLnk a:visited{ color:#333333; text-decoration:none; margin:0 10px 0 10px;}
  34. .footerLnk a:hover{ color:#ff0000; text-decoration:none;}

 

Further post that would you like to learn in Salesforce

 
 

 

FAQ (Frequently Asked Questions)

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.

 
   
 
 

Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment