Ajax reading XML to realize dynamic drop-down navigation

Time:2022-6-17

According to the needs of customers to do a product classification navigation menu, previously used ASP recursion to read. It is slow and consumes a lot of server resources. Simply change to ajax+xml. Share and communicate with everyone. I hope you can help improve.
XML file for product classification

Copy codeThe codes are as follows:
//ID is the self ID, and PID is the parent classification ID
<?xml version=”1.0″ encoding=”UTF-8″ ?> 
<Proot> 
<item id= “1” pid= “0” >1321 series </item>
<item id= “2” pid= “1” >43223 series </item>
</Proot> 

JS code

Copy codeThe codes are as follows:
var root;
//Firefox does not support the selectnodes method. Find this code on the Internet to solve this problem. Compatible with IE and firefox
//Create selectnodes method
if( document.implementation.hasFeature(“XPath”, “3.0”) )
{
   // prototying the XMLDocument
   XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
   {
      if( !xNode ) { xNode = this; } 
      var oNSResolver = this.createNSResolver(this.documentElement)
      var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
                   XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
      var aResult = [];
      for( var i = 0; i < aItems.snapshotLength; i++)
      {
         aResult[i] =  aItems.snapshotItem(i);
      }
      return aResult;
   }

   // prototying the Element
   Element.prototype.selectNodes = function(cXPathString)
   {
      if(this.ownerDocument.selectNodes)
      {
         return this.ownerDocument.selectNodes(cXPathString, this);
      }
      else{throw “For XML Elements Only”;}
   }
}

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        oXmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
    } 
    else if (window.XMLHttpRequest) {
        oXmlHttp = new XMLHttpRequest();
    }

}

function CreateXMLDOM()
{
createXMLHttpRequest();
oXmlHttp. Open (“get”, “XML path”, false);
oXmlHttp.send(null) ;
root = oXmlHttp.responseXML.documentElement;
}
CreateXMLDOM()

function funCreatePullMenu(passPid,ChildId)
{

var currentItems = root.selectNodes(“//Proot/Item[@pid = ” + passPid + “]”);
var iItems = currentItems.length;
var k=0;
if(iItems > 0)
{

  
  var pdiv = document.createElement(“DIV”);
  pdiv.id=”piv” + passPid;
  pdiv.className = “piv” + ChildId;
  pdiv.name = “piv” + passPid;
  if(passPid>0)
  {
       pdiv.style.display=”none”;
    document.getElementById(“div” + passPid).appendChild(pdiv);
  }
  else
  {
    document.getElementById(“odiv”).appendChild(pdiv);
  }
  for(var i = 0; i < iItems; i++)
  {
   var _id = currentItems[i].attributes[0].value;
   var newChild = document.createElement(“DIV”);
   newChild.id=”div” + _id;
   newChild.className = “div” + ChildId;
   newChild.name = “div” + _id;

   var _v ;
   if(CheckPullMenu(_id))
   {
    _1= _id
    _v = “<a href=’javascript:showsubmenu(” + _1 + “)’> ” +currentItems[i].firstChild.data + “</a>”;
   }
   else
   {
    _v = “<a href=’ProductList.aspx?type=” + _id + “‘>”+ currentItems[i].firstChild.data +”</a>”;
   }

   newChild.innerHTML=_v;

   
   document.getElementById(“piv” + passPid).appendChild(newChild);
   if(CheckPullMenu(_id))
   {
   funCreatePullMenu(_id,ChildId+1)
   }
  }
}
}

//Check whether there are subordinates
function CheckPullMenu(passPid)
{

var currentItems = root.selectNodes(“//Proot/Item[@pid = ” + passPid + “]”);
var iItems = currentItems.length;
if(iItems > 0)
{
  return true;
}
    else
{
  return false;
}

}

//Show hidden layers
function showsubmenu(sid)
{
var whichEl = document.getElementById( “piv” +sid);
if (whichEl.style.display == “none”)
  {
           whichEl.style.display=”block”;
  }
else
  {
     whichEl.style.display=”none”;
  }
}

Usage: add “<div></div>” to the web page. Add onload= “funcreatepullmenu (0,0)” to the body

Final effect:

You can add definition CSS to your code. Achieve better results.

Originating from:https://imgs.developpaper.com/imgs/200722821450856.gif’);}” alt=”” src=”/upload/200722821450856.gif” onload=”if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=’Click here to open new window\nCTRL+Mouse wheel to zoom in/out’;}” border=”0″>

You can add definition CSS to your code. Achieve better results.

Originating from:http://www.23mo.com/blog/article.asp?id=16