I have a static HTML web site. I want to store the the nav (top) menu in an external file so when I change the menu, I want to see the change in all pages.
I REALLY REALLY NEED to be able to see the web pages also locally (regular Windows user without the need to install additional software: Apache, PHP, Wamp, specific browsers, etc).
There are two similar StackOverflow questions that partially solves this issue using PHP, SSIand ... frames. None of the solutions is good for me:
- Frames out of discussion obviously (bad from SEO POV and also obsolete in HTML5).
- PHP and SSI will only work after the site was uploaded on the server so it won't work locally.
The solution I see is putting ALL the menu in an external JS file. However, all the JS examples I fave found still have some 'parts' of the menu in the HTML file.
So, it is possible to have all menu in a JS file and only a call (and no actual menu items) to that file in my HTML files? I only have basic knowledge of JS. But enough to adapt a generic example for my needs.
You can use JsRender/JsViews for this.
nav.html // you dont need to put html, body or etc. Just the nav itself.
index.html
TRY this. this might be the answer of your question.
A copy of my answer on a different thread:
I think this is the easiest way of doing it without a server side language. Use a javascript library like W3Data. All you need to do is add the w3-include-html attribute to your div. Done!
If your menu is in a file called menu.html you'd do something like this.
You can have a look into HTML Imports.
Option 1: In the simplest case you can do it like that:
index.html (or any other page):
nav.html:
More information at: http://www.html5rocks.com/en/tutorials/webcomponents/imports/
Option 2: Make full usage of the Web Components API and use stuff like your own HTML element, then the usage in all your files gets even easier (although the nav.html gets a little bit more complex).
index.html (or any other page):
nav.html
EDIT: A mentionable downside of both solution is the browser support:
So mainly it's Chrome (including Android) and Opera which support these features. Unfortunately it's not possible to use HTML imports without a web server because of security settings of the browser. You will get a console error like that:
So you would need to start a simple web server like the
nodejs
modulehttp-server
or use a Chrome extension like Chrome Dev Editor which brings a built in web server https://chrome.google.com/webstore/detail/chrome-dev-editor/pnoffddplpippgcfjdhbmhkofpnaalpg?utm_source=chrome-app-launcher-info-dialogmenu.html
index.html
js file:
The setTimeout lets the page load and then runs the function to see what link is active and then you can add a class in the css: