user5827260
user5827260

Reputation:

Making a menu with XML and javascript?

I was considering making a menu using XML and Javascript. But I am not sure, how to. I was thinking something like this Menu.xml

<menuroot>
<menu src="house.png" link="index.htm">img</menu>
<menu src="news.png" link="news.htm">img</menu>
<menu link="index.htm">1-3
<submenu link="1.htm"> 1</submenu>
<submenu link="2.htm"> 2</submenu>
<submenu link="3.htm"> 3
<submenu>31</submenu>
<submenu>32</submenu>
</submenu>
</menuroot>

All I want is an easy way for people who dont know programming to change the menu. Is this a smart way of doing this? Should I try something else? Any ideas? I also know of PHP if thats something I should rather use.

Upvotes: 0

Views: 171

Answers (2)

Kalpesh Kikani
Kalpesh Kikani

Reputation: 597

use the XML and Javascript

<p id="Menu"></p>
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        myFunction(xhttp);
    }
};
xhttp.open("GET", "Menu.xml", true);
xhttp.send();

function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    document.getElementById("Menu").innerHTML =
    xmlDoc.getElementsByTagName("menu")[0].childNodes[0].nodeValue;
}
</script>

Upvotes: 2

Kalpesh Kikani
Kalpesh Kikani

Reputation: 597

use the PHP function simplexml_load_file which will turn the XML into an object more..

<?php
$Menu = simplexml_load_file('menu.xml');
echo $Menu->item[0]->itemurl;
?

Upvotes: 0

Related Questions