最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Creating reusable html for navigation bar on multiple pages - Stack Overflow

programmeradmin8浏览0评论

I thought it would be convenient to have reusable code, especially for the navigation bar because it will be the same across all of my pages. This way I won't have to go through each page and manually edit each one individually when a change occurs.

It seems possible to use iframes, but I tried it and the whole page styling went out of whack. I can fix it but I'm wondering if there's something similar.

It would be awesome if something like this could work:

var navbar = document.getElementById('navbar');
navbar.innerHtml = url('navigation.txt');

I'm currently working offline on my site so I don't think I can make xmlhttp requests. Correct? At least I still have yet to get any ajax example to work. This is unfortunate because I think I could easily use this for my application.

Here's my navbar markup. It's not very plicated so I have a feeling I'll just edit it manually in the end.

<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>
</nav>

I thought it would be convenient to have reusable code, especially for the navigation bar because it will be the same across all of my pages. This way I won't have to go through each page and manually edit each one individually when a change occurs.

It seems possible to use iframes, but I tried it and the whole page styling went out of whack. I can fix it but I'm wondering if there's something similar.

It would be awesome if something like this could work:

var navbar = document.getElementById('navbar');
navbar.innerHtml = url('navigation.txt');

I'm currently working offline on my site so I don't think I can make xmlhttp requests. Correct? At least I still have yet to get any ajax example to work. This is unfortunate because I think I could easily use this for my application.

Here's my navbar markup. It's not very plicated so I have a feeling I'll just edit it manually in the end.

<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>
</nav>
Share Improve this question asked Jan 5, 2011 at 17:27 ptpatersonptpaterson 9,5834 gold badges30 silver badges41 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 3

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./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 mand to pile 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 piler that generates Java google closure methods. You can look for it on their website.

Hope it helps.

Use a server side language to create a navigation file. It can be static or it can be extremely plex, it's up to you.

<?php include 'includes/nav.php'; ?>

contents of nav.php can be the <nav> element entirely. You can ideally program it to show/hide elements based on the current "section", and also toggle certain classes based on the section.

You very much can use AJAX calls "offline" as you put it, it's client side code.

But the way I'd do this if I wasn't using a server side language (ASP.NET or PHP) is to have a small .js file that renders the navbar, and I'd just add a <script src='js/navbar.js'></script> where the navbar would go.

This way when you need to change it, you only change the .js and it would update in every other page.

A few suggestions:

  • If you're using JSP (or similar technology), you can simply use something like SiteMesh here to help with the templating
  • You can use server side includes
  • You can write a JavaScript function to programmatically build the DOM you need (no AJAX required) and then just call that JS function throughout your pages (no duplication of code)

If you are going to use the same code over and over, it might be best to create a separate file and implement it in different webpages.

It is possible through this: https://www.w3schools./howto/howto_html_include.asp

You write the nav bar's html in a separate .html file and call it in like this:

<div w3-include-html="content.html"></div>

Then call in a javascript function from

<script src="https://www.w3schools./lib/w3data.js"></script> 

which is w3IncludeHTML();

And you should have it up and running! Hope this helped! :)

Normally these kinds of re-usable pieces of mark-up will be generated server side, mostly through a templating engine.

You could change Firefox's security settings to allow for offline AJAX calls, but it is probably better to set up a local development environment where you can start using a server side language, and do it properly from the start.

If you want to implement it without any server side coding involved, try this:

var http = false;

if(navigator.appName == "Microsoft Internet Explorer") {
  http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
  http = new XMLHttpRequest();
}

http.open("GET", "navigation.txt");
http.onreadystatechange=function() {
  if(http.readyState == 4) {
    var navbar = document.getElementById('navbar');
        navbar.innerHtml = http.responseText;
  }
}
http.send(null);
发布评论

评论列表(0)

  1. 暂无评论