How to Reuse a Navigation Bar on Multiple Pages

How to reuse a navigation bar across different .html pages with CSS and JS?

Honestly, there's not an easy way to do this with vanilla Javascript. You basically have to either create the navbar using JS, which can then be imported to each page, or you have to copy and paste it.

The industry solution to this problem is to use React, Vue or Angular, all technologies which allow you to create and reuse JSX 'components'. In other words, in React, you could create a navbar component, which could easily be imported in any 'page' of the website. But with vanilla HTML/Javascript, it's really not an option.

How can I re-use the navigation bar in multiple pages (Bootstrap)

The functionality you are looking for is referred to as templating. Templating allows you to create blocks of HTML that can be included and re-used various places throughout a web page/site.

For static content (plain HTML pages), Gatsby, Hugo, and Jekyll are all good choices with lots of documentation and strong community support.

Single navigation bar across website?

JQuery

As JQuery is JS-based, you might be allowed to use it. You could then add a navigation div into each page's template:

<div id="navigation"></div>

and include a script on each page that executes the following JQuery-code:

$(function() {
$("#navigation").load("navigation.html");
});

Pure JavaScript

Alternatively, if you cannot use JQuery whatsoever, you could use plain JavaScript, which is more lightweight but not as clean.

Wherever you want to include the navigation, simply include a script:

<script src="nav.js"></script>

Which holds your navigation based on document.write:

document.write('<div>\
... your navigation content ...\
</div>\
');

Creating reusable html for navigation bar on multiple pages

Like it's been said, typically this is done server side with an include for non AJAX sites. However, I think you can make use of google closure templates. Basically, you define a template in their templating language, that generates a javascript function you can call to render your HTML.

http://code.google.com/closure/templates/docs/helloworld_js.html

Example:

--templates.soy

{namespace templates}

{template .nav}
<ul id="navbar">
<li><a href="biosketch.html">Biosketch</a></li>
<li><a href="projects.html">Class Projects</a>
<ul>
<li><a href="projects.html#SeniorProject">Senior Project</a></li>
<li><a href="projects.html#WindTurbine">Wind Turbine</a></li>
</ul>
</li>
<li><a href="#">Resume</a></li>
<li><a href="#">Work Experience</a></li>
<li><a href="#">Contact Me</a></li>
</ul>
{\template}

Then you run the following command to compile it into a javascript function

java -jar SoyToJsSrcCompiler.jar --outputPathFormat templates.js  templates.soy

This will generate a file called templates.js containing a function called templates.nav which you can call from your page like the following:

document.getElementById('navbar').innerHTML = templates.nav();

This is not even using the data merging, which would allow you to pass in a data object to render HTML that is not static. But I've only shown you this since it's all you asked for. I know you could just paste the html into a JS string also, but you's have to deal with the lack of syntax help from your editor.

The one drawback is that this requires JS which you don't seem to mind. However, if you wanted to support JS-less clients, you could generate the template on the server side. There is also a compiler that generates Java google closure methods. You can look for it on their website.

Hope it helps.

How can I reuse and html navbar on each page of my website?

On your site you are loading jquery in the head and then loading jquery slim in the foot of your page. In jquery slim load() is not supported. Instead of loading 2 versions of jquery lust load the full version of jquery in your foot section and place your jquery code after that. Here is how your page should look:

<!DOCTYPE html>
<html lang="en">
<head>

</head>

<body>

<!--Navigation bar-->
<div id="nav-placeholder">

</div>


<!--end of Navigation bar-->


<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script>
$(function(){
$("#nav-placeholder").load("navbar.html");
});
</script>
</body>

Github pages - how to reuse html code on each page (like a nav bar)

Github-pages uses Jekyll, which on its own uses Markdown and Liquid.

If I've understood you correctly,

You can achieve what you want by using include syntax in Liquid which is referenced here.

You can find more information about include in here and this blog post.

This project uses different scenarios of include.

Github-pages is a powerful static site generator and you can achieve a lot by using it, checkout this list.



Related Topics



Leave a reply



Submit