I would like to make my dropdown menu updateable from a single file.
Let me explain: On my website I have a general menu, this menu is in a file called nav.html every time I add a new section to my website, I just have to go to this file nav.html and there I put my new section This prevents me from going to each tab of the web and putting the new section one by one . I want to do the same thing but with a dropdown menu that I have. If someone could tell me how to make this dropdown section updateable from a single file that would be great. It would be a single file where I would put each new section.
I put what I use now:
<SCRIPT LANGUAGE="JavaScript">
function salta(Sel)
{
if (Sel.ad.selectedIndex != 0)
{
document.location=Sel.ad.options[Sel.ad.selectedIndex].value
}
}
</SCRIPT>
<div>
<form>
<select name="ad" onchange="salta(this.form)">
<option selected>Artículos:
<option value=“articulo1.html”>ejemplo 1
<option value="articulo2.html”>Ejemplo 2
</select>
</form>
</div>
What I want is to have a file somewhere that is easily updatable. If I am adding articles to my dropdown, and now I already have 8 articles, every time I put a new file, I am forced to open the 8 articles, which are 8 different html files. When I'm 25 it's going to be a hassle to open them all one by one. I think the best has to be an html or javascript file where my editable menu is.
Instead of making a document,
HTML
I suggest you make onescript
that creates the dropdown and inserts it where you want. Something like this:Since some of these things don't work in some browsers, you can convert this code to ES5 from here Babeljs.io
I leave you a couple of alternatives based on what you say in the comments below. The two are similar, they consist of having the HTML in the dropdown.html file and then loading its content using JavaScript (for simplicity I use jQuery and the method
load()
but it could be done with pure JavaScript and an AJAX call).Common to both alternatives
Where you want the dropdown to go, put an
div
empty with id "dropdown" (for example) like so:So after jQuery has loaded you can load dropdown.html with the load method like so:
And that will load the content of dropdown.html into the element with id "dropdown".
Alternative 1: HTML + JavaScript
If you have a file with JavaScript code, add a function that is to redirect the page:
And your dropdown.html file would look something like this:
Alternative 2: HTML + CSS
In this alternative you don't need to have the JavaScript function in your code (except loading the page with AJAX) because everything will be done with HTML and CSS. The content of dropdown.html could be something like this (I make it executable so you can see that it simulates a dropdown list):
And since they are links, you will no longer need any extra JavaScript code. In fact, you could add the CSS to your own CSS file and it would make the dropdown menu load faster. You just have to customize it.